I have a 'User' model which has a has_many relationship to a 'Number' model through a join table 'user_number' model. I use:
accepts_nested_attributes_for :numbers, :allow_destroy => true
in the 'User' model. Everything works fine except that whene开发者_如何学Gover I delete a number from a user in the edit form, the associated number is deleted correctly in the 'number' table, but not the entry in the 'user_number' join table.
In the update controller action I only use this:
...
if @user.update_attributes(params[:user])
...
How can I force rails to also delete the associated entry in the join table?
You need to have User accepts_nested_attributes_for :join_class, :allow_destroy => true. Then, you delete the association. The idea behind a has_many :through is that you don't delete the m part of the n->m mapping unless there are no other associations left..
精彩评论