开发者

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

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_man开发者_JAVA百科y :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:

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

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 :)


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜