has_one and belongs_to-like associations

34 views
Skip to first unread message

davidkpham

unread,
Dec 22, 2011, 8:59:45 PM12/22/11
to CouchRest
Hello,

How does one replicate the has_one and belongs_to-like associations
with CouchRest and CouchRest Model?

I would like to be able to do things like:

user.build_subscription(:subscription_attribute => 'Example')

and

subscription.user.method

Supposing that User has_one Subscription and Subscription belongs_to
User.

Thanks,
David

Stephen Benner

unread,
Nov 21, 2013, 9:35:38 PM11/21/13
to couc...@googlegroups.com, david....@gmail.com
Hi David,

I'm not an expert but I'm working hard on developing my own library based on CouchRest/CouchModel so I know a lot about it already. What you're talking about is persistence of the association metadata in particular. So one thing that's vital to grasp in the beginning is the difference between what's stored in the database and the Ruby models/data. Of course you are just storing the properties and you have to include methods like 'timestamps!' to add metadata to the actual JSON document in CouchDB. So to 'replicate' an association you would have to represent it in a form of data, and there are many ways to do so.

At this point though it's probably going to be difficult without a good understanding of database concepts, for example the principle of CouchDB 'views' are really important here. You could store model IDs in the model docs, since that might make sense looking at how the Ruby objects store arrays of foreign keys and such. But that is FAR less effective than using views; with views you can store relational data like id's in a way that scales with the rest of your content and won't intrude into regular document data, because views can compose info from multiple docs instead of being confined to just one.

Look into (google it) the CouchDB guide on this, it is really excellent to explain the principles.
Reply all
Reply to author
Forward
0 new messages