无法在连接表中为rails中的has_many_through关联保存数据

时间:2013-06-27 17:22:47

标签: ruby-on-rails ruby associations

我有用户,团队和team_user模型。 team_use通过用户和团队之间的关联作为has_many的结果加入模型。 team_user具有来自用户和团队表的user_id和team_id。团队有name和team_lead_id(用户表中的用户)。

问题:

当我创建新团队时,我希望将用户和团队负责人添加到团队中。一个团队可以拥有多个用户,但只有一个团队领导。创建团队时,user_id和team_id应保存在team_user表中。但这不会发生。我尝试使用team_controller.rb中的以下代码:

def create
    @team = Team.new(params[:team])
      @user_team = TeamUser.new( { user_id: '@user.id', team_id: 'params[:team_id]' } )
      if @team.save
        flash[:notice] = 'Team has been created'
        redirect_to teams_path
      else
        flash[:alert] = 'Team not created'
        redirect_to teams_path
      end
  end

使用TeamUser.new( { user_id: '@user.id', team_id: 'params[:team_id]' } ),我尝试在team_user表中创建用户和团队条目但失败了。

代码如下。

teams_controlller.rb

class TeamsController < ApplicationController
  before_filter :authorize_admin!

  def index
    @teams = Team.all
    @team = Team.new
  end

  def new
    @team = Team.new
  end

  def create
    @team = Team.new(params[:team])
      @user_team = TeamUser.new( { user_id: '@user.id', team_id: 'params[:team_id]' } )
      if @team.save
        flash[:notice] = 'Team has been created'
        redirect_to teams_path
      else
        flash[:alert] = 'Team not created'
        redirect_to teams_path
      end
  end

  def show
    @team = Team.find(params[:id])
  end

  def edit
    @team = Team.find(params[:id])
  end

  def update
    @team = Team.find(params[:id])
      if @team.update_attributes(params[:team])
        flash.notice = "Team #{@team.name} has been updated"
        redirect_to team_path
      else
        render 'edit'
      end
  end

  def destroy
    @team = Team.find(params[:id])
    @team.destroy
    redirect_to action:  'index'
  end

end

team.rb

class User < ActiveRecord::Base
 has_many :roles,  through: :role_users
 has_many :role_users
 has_many :leaves
 serialize :role
 has_many :teams, through: :team_users
 before_save :make_array
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :username, :email, :password, :password_confirmation,
  :remember_me, :first_name, :last_name, :is_admin, :contact_no, :birth_date,
  :joining_date, :is_active, :role, :is_manager, :user_code, :designation
  # attr_accessible :title, :body

   def active_for_authentication?
     super && is_active?
   end

  def make_array
   self.role.reject!(&:blank?) if self.role
  end

end

team_user.rb

class TeamUser < ActiveRecord::Base
  belongs_to :user
  belongs_to :team
  attr_accessible :team_id, :team_lead_id, :user_id
end

team.rb

class Team < ActiveRecord::Base
  has_many :users, through: :team_users
  has_many :team_users
  attr_accessible :name, :team_lead_id
end

_form.html.erb(组)

<%= form_for @team do |f| %>
  <% @team.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>

  <div style=" margin-top:10px">
    <label> Team Name </label>
    <%= f.text_field :name, :class => 'text_field' %>
  </div>

  <label> Add Users </label>
  <%= select_tag "TeamUser[user_id]", options_from_collection_for_select( User.all, "id", "first_name"), :style => "width:270px; height:35px", :id => "drp_Books_Ill_Illustrations",
      :class => "leader MultiSelctdropdown Books_Illustrations" %>

   <label> Team Lead </label>
  <%= f.select(:team_lead_id, User.all.map { |u| [u.first_name, u.id] }) %>

  <div class=modal-footer>
    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
    <%= f.submit "Create Team", :class => 'btn btn-primary' %>
  </div>
<% end %>

schema.rb

 create_table "team_users", :force => true do |t|
    t.integer  "team_id"
    t.integer  "user_id"
    t.datetime "created_at",   :null => false
    t.datetime "updated_at",   :null => false
  end

  create_table "teams", :force => true do |t|
    t.string   "name"
    t.datetime "created_at",   :null => false
    t.datetime "updated_at",   :null => false
    t.integer  "team_lead_id"
  end

我可以在团队模型中使用team_lead_id创建团队,但无法在team_user团队中保存数据。

1 个答案:

答案 0 :(得分:2)

没有保存的原因是您拨打new而不是create。此外,当您尝试创建TeamUser记录时,您不知道@team.id但此时尚未保存。但还有其他事情需要修复。首先,您的new操作应该是:

def new
  @team = Team.new
  @users = User.all
end

现在您不需要在视图中两次调用User.all,这不是很好的做法。 现在,观点:

  <%= form_for @team do |f| %>
  <% @team.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>

  <div style=" margin-top:10px">
    <label> Team Name </label>
    <%= f.text_field :name, :class => 'text_field' %>
  </div>

  <label> Add Users </label>
  <%= select_tag "users[]", options_from_collection_for_select( @users, :id, :first_name), :style => "width:270px; height:35px", :id => "drp_Books_Ill_Illustrations",
      :class => "leader MultiSelctdropdown Books_Illustrations", :multiple => true %>

   <label> Team Lead </label>
  <%= select_tag(:team_lead_id, options_from_collection_for_select(@users, :id, :first_name)) %>

  <div class=modal-footer>
    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
    <%= f.submit "Create Team", :class => 'btn btn-primary' %>
  </div>
<% end %>

create行动中:

def create
  @team = Team.new(params[:team])
  team_lead = User.find(params[:team_lead_id])
  @team.team_lead = team_lead
  if @team.save
    users = User.where(:id => params[:users])
    users.each {|user| @team.users << user}
    flash[:notice] = 'Team has been created'
    redirect_to teams_path
  else
    flash[:alert] = 'Team not created'
    redirect_to teams_path
  end
end

您还应该更新Team模型:

class Team < ActiveRecord::Base
  has_many :users, through: :team_users
  has_many :team_users
  belongs_to :team_lead, class_name: 'User'
  attr_accessible :name
end

由于您不仅在new操作中显示表单,我建议您在控制器中设置before_filter,以设置表单所需的变量:

before_filter :set_form_variables, only: [:new, :index] # everywhere the form is displayed
# ...
private
  def set_form_variables
    @team = Team.new
    @users = User.all
  end