I am having a bunch of problems with validates_associated when
updating a record, and I think I have discovered the problem. If I
have two models in a has_many relationship, if I use the << operator
on a new master record, the slave records are not automatically saved
until I call master.save. However, on an existing master record,
the mere act of associated a new many record causes that many record
to be saved.
What is the reasoning for this?
Can I disable it and have the many’s save only when I save the
master?
Have a look at the Unsaved Associations section athttp://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMet…
Because I want to associate a mix of already saved, and new tags, what
I wound up doing was for each tag, if it had an id (is there a saved?
method?) I just associate it, otherwise, I build it, passing the tags
attributes. This works, although I suspect there’s a better solution.
What I’m trying to accomplish is to have a one-to-many on one form and
I want to just push the errors for the many’s up to the one’s errors.
So I added validates_associated, and it works fine on a new record.
On an existing record, as soon as I associate the many record with the
one, the many record saves, and if it’s invalid, it throws an
exception, so the one record never even validates (so no error ever
propagates to the one). Let me know if you have any thoughts on
this.
Thanks again,
Dino
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.