Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

association_ids is not updated when all options are unselect

#2336

class User < AR has_many_belongs_to :roles end

in User model Edit view

<%Role.find(:all,:select=>"id,name").each do |role|%> <%=check_box_tag name="user[role_ids][]",role.id,@user.role_ids.include?(role.id)%><%=role.name%> <%end%>

in has_many :through (or) has_and_belongs_to_many association

when editing user details if you unselect all role name(means no role assigned to that user like public user ) options then it won't update in the intermediate (or) join_table

because params[:user][:role_ids] is nil

to avoid that you can add one more hidden with zero length string value like this

<%Role.find(:all,:select=>"id,name").each do |role|%> <%=check_box_tag name="user[role_ids][]",role.id,@user.role_ids.include?(role.id)%><%=role.name%> <%end%>

Reported by thaniyarasu · March 25th, 2009 @ 10:09 AM

State: invalid
Milestone: 2.x
Assigned to: Ryan Bigg Ryan Bigg
Importance: none

Activity

  1. thaniyarasu
    thaniyarasu

    <%Role.find(:all,:select=>"id,name").each do |role|%> <%=check_box_tag name="user[role_ids][]",role.id,@user.role_ids.include?(role.id)%><%=role.name%> <%end%>

    March 25th, 2009 @ 10:11 AM

  2. David Trasbo
    David Trasbo
    • Assigned user set to Ryan Bigg

    If you, in this example, set role_ids to nil the join table will be updated. I think you're having this issue because the role_ids parameter will not even get sent to the controller when no check boxes are checked. It's not nil - it's undefined. This can be marked as invalid.

    April 15th, 2010 @ 07:00 PM

  3. Ryan Bigg
    Ryan Bigg
    • State changed from new to invalid

    April 15th, 2010 @ 11:00 PM