0

I'm trying to create a basic Rails CRUD app against a CouchDB database hosted on Cloudant.

I'm using couch_potato as my persistence layer and have it connecting properly to my Cloudant database.

The issues I'm having is my first model won't see the existing documents in my CouchDB database, unless I add a ruby_class field that equals the name of my model.

My simple User model:

class User
  include CouchPotato::Persistence

   property :id, :type => Fixnum
   property :FullName, :type => String

   view :all, :key => :FullName
end

Sample CouchDB document:

{
  "_id": 123456,
  "_rev": "4-b96f36763934ce7c469abbc6fa05aaf3",
  "ORGID": 400638,
  "MyOrgToken": "19fc342d50f9d8df1ecd5e5404f5e5f7",
  "FullName": "Jane Doe",
  "Phone": "555-555-5555",
  "MemberNumber": 123456,
  "Email": "jane@example.com",
  "LoginPWHash": "14a3ccc0e6a50135ef391608e786f4e8"
}

Now, when I use my all view from the rails console, I don't get any results back:

1.9.2-p290 :002 > CouchPotato.database.view User.all
=> []

If I add the field and value "ruby_class: User" to the above CouchDB document, then I get results back in the console:

1.9.2-p290 :003 > CouchPotato.database.view User.all
=> [#<User _id: "123456", _rev: "4-b96f36763934ce7c469abbc6fa05aaf3", created_at: nil,       
   updated_at: nil, id: "123456", FullName: "Jane Doe">]

I'm working with a large set of customer data, and I don't want to write any scripts to add the ruby_class field to every document (and I may not be permitted to).

How can I get my app to recognize these existing CouchDB documents without adding the ruby_class field?

I couldn't find much documentation for couch_potato and couchrest that shows how to work with existing CouchDB databases. Most of the examples assume you're starting your project and database(s) from scratch.

Thanks,

/floatnspace

4

1 回答 1

0

when you are looking at the all view of your User you will see something like ruby_class == 'User' so unless you add this property to your documents you will need to work around what couch_potato provides. you could i.e. use couch_rest directly to retrieve your documents, but i don't think that this what you want.

if you start persisting or updating your own documents, couch_potato will add the ruby_class field anyways. so i think the simples solution would be to just add them there.

another thing you can do is create a view that emits the documents also when they DON'T have the property set. this approach will only work if you have just one kind of document in your couchdb:

if(!doc.ruby_class || doc.ruby_class == 'User') {
  emit(doc);
}
于 2012-03-12T09:54:30.097 回答