Creating many-to-many relationships on a nested rails form

旧街凉风 提交于 2020-01-01 14:07:13

问题


I am trying to create Groups, Users and Memberships (the many-to-many relationships) simultaneously. People can add users to the group as they create it and then I want it to route through to a view of the group with all the members. I can get the users to be created and the current user's membership to save to the database. However I'm struggling to get the id for the freshly created User objects in order to save the new memberships.

Group.rb

class Group < ActiveRecord::Base
  has_many :memberships
  has_many :users, :through => :memberships
  has_many :users

  accepts_nested_attributes_for :users, :reject_if => lambda { |a| a[:name].blank? or a[:number].blank? }
end

Membership.rb

class Membership < ActiveRecord::Base
  belongs_to :groups
  belongs_to :users
end

User.rb

class User < ActiveRecord::Base
  has_many :memberships
  has_many :groups, :through => :memberships
  belongs_to :group
end

groups_controller.rb

class GroupsController < ApplicationController

  def show
    @group = Group.find(params[:id])    
    respond_to do |format|
      format.html
    end
  end

  def new
    @group = Group.new

    3.times { @group.users.build }
  end

  def create 
    @group = Group.new(params[:group]) 
    @group.memberships.build(:user_id => current_user.id)
    logger.info @group.users

    @group.users.each do |x|
      @group.memberships.build(:user_id => x.id )
    end

    respond_to do |format|
      if @group.save
        format.html { redirect_to(@group, :notice => 'Success!') }
      else
        format.html { render :action => "new" }
      end
    end

  end
end

My form looks like this:

Which is created by:

views/groups/new.html.rb

<h1>New group</h1>

<%= form_for(@group) do |form| %>
<fieldset>
  <legend>Create a new group</legend>
  <%= render 'shared/group_error_messages', :object => form.object %>  
  <div class="clearfix">
    <%= form.label :group_name %>
    <div class="input">
      <%= form.text_field :group_name %>
    </div>
  </div>

  <div id="users">
    <div class="user">
      <%= form.fields_for :users, :url => {:action => 'new', :controller => 'users', :registered => false}, do |user_form| %>
      <%= render 'user', :user => user_form %>
      <% end %>
    </div>
  </div>
</div>

<div class="actions">
  <%= form.submit :value => 'Create your group', :class => 'btn success'%>
</div>
</fieldset>
<% end %>

views/groups/_user.html.rb

<div class="clearfix">
  <%= user.label :name %>
  <div class="input">
    <%= user.text_field :name %>
  </div>
</div>
<div class="clearfix">
  <%= user.label :number %>  
  <div class="input">
    <%= user.text_field :number %>
  </div>

</div>
<div class="clearfix" style="display:none">
  <%= user.label :password %>  
  <div class="input">
    <%= user.password_field :password, :value => newpass(6) %><!-- todo: fix this horrible hack -->
  </div>
</div>
<div class="clearfix" style="display:none">
  <%= user.label :registered %>  
  <div class="input">
    <%= user.text_field :registered, :value => false %> <!-- todo: fix this horrible hack -->
  </div>
</div>
<div class="clearfix" style="display:">
  <%= user.label :id %>  
  <div class="input">
    <%= user.text_field :id %> <!-- todo: fix this horrible hack -->
  </div>
</div>

So my main desire is to get this bit of code to find the User ids as they are created and then use those ids to build the memberships between the groups and users.

Thanks very, very much in advance :)


回答1:


I think

has_many :users, :through => :memberships
has_many :users

is wrong, you can't have two or more associations with same name .

Try to do

has_many :users, :through => :memberships
has_many :members, :class_name => 'User', :foreign_key => :user_id

and make corresponding changes in views and controllers . Like

form.fields_for : members


来源:https://stackoverflow.com/questions/7266952/creating-many-to-many-relationships-on-a-nested-rails-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!