开发者

Rails 3 nested forms with has_many :through, entry in join table doesn't get deleted after update

开发者 https://www.devze.com 2023-01-20 16:02 出处:网络
I have a \'User\' model which has a has_many relationship to a \'Number\' model through a join table \'user_number\' model.

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..

0

精彩评论

暂无评论...
验证码 换一张
取 消