has_one and belongs_to-like associations

34 просмотра
Перейти к первому непрочитанному сообщению

davidkpham

не прочитано,
22 дек. 2011 г., 20:59:4522.12.2011
– 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

не прочитано,
21 нояб. 2013 г., 21:35:3821.11.2013
– 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.
Ответить всем
Отправить сообщение автору
Переслать
0 новых сообщений