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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
304987a0988db3c626ea6be2a27bb03b?d=identicon&s=25 tvinod@gmail.com (Guest)
on 2009-05-11 18:44
(Received via mailing list)
Hi,
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 =>
"Group_id"
...

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][]", g.id,
@user.groups.include?(g)) %>
        <%= "#{g.name}"%><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 %>
</div>

Any ideas how I can do this?

thanks
This topic is locked and can not be replied to.