This project is archived and is in readonly mode.

#2336 ✓invalid
thaniyarasu

association_ids is not updated when all options are unselect

Reported by thaniyarasu | March 25th, 2009 @ 10:09 AM | in 2.x

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

Comments and changes to this ticket

  • thaniyarasu

    thaniyarasu March 25th, 2009 @ 10:11 AM

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

  • David Trasbo

    David Trasbo April 15th, 2010 @ 07:00 PM

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

  • Ryan Bigg

    Ryan Bigg April 15th, 2010 @ 11:00 PM

    • State changed from “new” to “invalid”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Referenced by

Pages