You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been going through the README and rdocs, and I'm not sure what the recommended way is to "create or update" a doc. I'm looking for something like Rails' find_or_create_by but for couchrest.
My Model
classUser < ApplicationRecordhas_many:eventsdefcopy_events_to_pouchdb!puts"Copying Events for user #{id} to pouchdb"server=CouchRest.new("http://localhost:5985")db=server.database!("events-user_#{id}")events.eachdo |event|
puts" Copying Event #{event.id} to pouchdb"doc=CouchRest::Document.new(_id: event.id.to_s,title: event.title,)db.save_doc(doc)endendend
My Rake task
desc"Copy all users to pouchdb-server"taskcopy_events_to_pouchdb: :environmentdoUser.find_eachdo |user|
user.copy_events_to_pouchdb!endend
The first time this runs, it works and the events are created as expected, but the second time it runs I get
Do I need to always go t get THEN a put? (because that would produce a _rev field for the document?) The rdocs for Database#save_doc and Document#save imply that it can create or update, but I don't see any clear-cut examples of how to do this.
Thanks again for the time you spend working on this gem.
The text was updated successfully, but these errors were encountered:
aguynamedben
changed the title
What is the proper way to create or update a doc?
What is the recommended way to create or update a doc?
Jul 31, 2017
aguynamedben
changed the title
What is the recommended way to create or update a doc?
What is the recommended way to "create or update" a doc?
Jul 31, 2017
One method is to approximate Rails' find_or_intialize_by like this:
defcopy_events_to_pouchdb!puts"Copying Events for user #{id} to pouchdb"server=CouchRest.new("http://localhost:5985")db=server.database!("events-user_#{id}")events.eachdo |event|
puts" Copying Event #{event.id} to pouchdb"# First HTTP requestdoc=db.get(event.id.to_s) || CouchRest::Document.new(_id: event.id.to_s)doc["title"]=event.title# Second HTTP requestdb.save_doc(doc)endend
The downside is this requires 2 HTTP requests instead of 1.
Hi, thanks for your work on this excellent gem.
I've been going through the README and rdocs, and I'm not sure what the recommended way is to "create or update" a doc. I'm looking for something like Rails'
find_or_create_by
but for couchrest.My Model
My Rake task
The first time this runs, it works and the events are created as expected, but the second time it runs I get
Do I need to always go t get THEN a put? (because that would produce a
_rev
field for the document?) The rdocs forDatabase#save_doc
andDocument#save
imply that it can create or update, but I don't see any clear-cut examples of how to do this.Thanks again for the time you spend working on this gem.
The text was updated successfully, but these errors were encountered: