Forum: Ruby on Rails updating join table (HMT) with checkbox and extra fields

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
on 2009-05-11 20:44
(Received via mailing list)
I've been breaking my head over this thing with no success. Here is my
table layout -

class User < AR
has_many :memberships
has_many :groups, :through => :memberships

class Group < AR
has_many :memberships
has_many :users, :through => :memberships

class Membership < AR
  belongs_to :user, :class_name => "User", :foreign_key => "user_id"
  belongs_to :group, :class_name => "Group", :foreign_key =>

Now, Membership table has an extra field called membership_id and I
want the user to fill in that id in the edit form.

My view looks like this. The checkbox fields works great and I can
update my db pretty well (barring minor issues).

<% form_for :user, @user, :url => { :action => 'save_memberships'} do |
form| %>
  <%= form.error_messages %>
      <% @groups.each do |g| %>
        <%= check_box_tag("user[membership_ids][]",,
@user.groups.include?(g)) %>
        <%= "#{}"%><br>
----> I have to invoke my partial to collect membership id here. But
can't get the call right. The partial is below.
      <% end%>
<% end%>

This is the membership partial -
<div class="membership">
  <%  fields_for "user[membership_ids][]", membership do |f| %>
    Membership id: <%=  f.text_field :membership_id %>
  <% end %>

Any ideas how I can do this?

This topic is locked and can not be replied to.