couchrest / couchrest_model

Doing the simple stuff so you don't have to
Apache License 2.0
304 stars 116 forks source link

2 models referencing each other need 2 assignements #191

Open yarmand opened 9 years ago

yarmand commented 9 years ago

With relational DB, a 1-N relation only need a foreign key to retrieve relation content on both side. With documents DB a search on a foreign key is non efficiant. My proposal is to automaticaly assign a reference on both sides. See more details in this pull request

samlown commented 9 years ago

Hey. Thanks for contributing this! I like the look of what you're doing and would like to check it out as soon as possible.

Given the atomic or non-transactional nature of CouchDB, I'm slightly reluctant to add this as a feature that is activated by default. I wouldn't want to give the false impression of consistency. There is no guarantee that both documents would be saved at the same time, even less so if there is validation on both models.

yarmand commented 9 years ago

yes, I was wondering the same thing. I was thinking of triggering back assignement only when there is an explicit usage of the :reverse_association.

About validation. When object A is saved, it will trigger a save on object B. Any failed validation on A will block the process. Most of the time B will come from a db read to get updated and saved. The only problematic case I see it the creation of both object at the same time, so B can be affected to an association of A before being validated once. There is a way to detect this with B.new?, but I'm not sure about the what to do here: