pop
pop copied to clipboard
Eager Update.
If the parent already exists the children cannot be eagerly updated.
Eager Destroy would be fine as well.
@stanislas-m
Here's the spec requested in #318
- If the association is has-one, eagerly updating the parent model will update the child using fk_id (not considering id of the child in the given parent to make sure there's only one child). If the child doesn't exist in the database, it will create one using the given model.
- If the association is has-many, eagerly updating the parent model will save every child and delete children in the database that don't exist in the given parent.
- If the association is many-to-many, eagerly updating the parent model will link new children and unlink absent children in the link table and save every child.
- The identity of children will be determined by their ids.
- Eager update will eager update nested models in multiple levels.
- Flat updating the model will link new children and unlink absent children in the link table if the association is many-to-many.
If there's something to change, let me know.
@sunho I didn't forget about this, I'm just laking of time right now. I'll hope to find some time this month.
@stanislas-m
Here's the spec requested in #318
* If the association is has-one, eagerly updating the parent model will update the child using fk_id (not considering id of the child in the given parent to make sure there's only one child). If the child doesn't exist in the database, it will create one using the given model.
👍
* If the association is has-many, eagerly updating the parent model will save every child and delete children in the database that don't exist in the given parent.
👍
* If the association is many-to-many, eagerly updating the parent model will link new children and unlink absent children in the link table. It will not change the children in the database.
And create children that doesn't already exist?
* The identity of children will be determined by their ids.
👍
* Eagerly updating a parent will eagerly update the children if the association is not many-to-many.
Is this referring to multiple levels of nesting in the update or?
If there's something to change, let me know.
This is pretty close to what we had in mind for the "Eager" update but what about the "Flat" update? What do you propose is done about nested object/association updates when there is no explicit "Eager"?
Thanks so much for writing this out! Looking good so far!
@mclark4386
-
I think it would be useful to cause an error if the children don't exist in a many-to-many relationship. Because it is usually used to describe the relationship between the models that already exist.
-
Yes, I was talking about multiple levels of nesting. Like category > article > comment.
-
I think flat update should not care about the association since flat loading doesn't care about it.
- Interesting, you can't think of a case it would be useful? (the books test in the pop association tests comes to mind)
- Gotcha.
- Flat Create does create associations, but not nested objects.
Oh I just read the code and finally got what you've been talking about! Then it would be reasonable to save children in many-to-many if it's an eager connection. I edited the earlier post, please take a look.
And I think there should be a way to disable flat update and create, because it can be unwanted for some people. I personally think making the default connection not to do flat update and create is idiomatic, but that would break the api. How about supporting rw tag for associations?
@stanislas-m
Here's the spec requested in #318
* If the association is has-one, eagerly updating the parent model will update the child using fk_id (not considering id of the child in the given parent to make sure there's only one child). If the child doesn't exist in the database, it will create one using the given model. * If the association is has-many, eagerly updating the parent model will save every child and delete children in the database that don't exist in the given parent. * If the association is many-to-many, eagerly updating the parent model will link new children and unlink absent children in the link table and save every child. * The identity of children will be determined by their ids. * Eager update will eager update nested models in multiple levels. * Flat updating the model will link new children and unlink absent children in the link table if the association is many-to-many.
If there's something to change, let me know.
@stanislas-m this looks good to me, would you mind giving a once over when you get a chance?
We've just hit this case too. Happy to help wherever/however we can!
So... any updates here?
I've completely forgotten about this. I don't really have time to work on this nowadays. Please feel free to take over if anyone is interested in this feature.