我有用户、团队和 team_user 模型。由于 has_many 通过用户和团队之间的关联,team_use 正在加入模型。team_user 具有来自用户和团队表的 user_id 和 team_id。团队有名称和 team_lead_id(这将是用户表中的用户)。
问题:
当我创建新团队时,我想将用户和团队领导添加到团队中。一个团队可以有多个用户,但只有一个团队负责人。在创建 team 时, 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 表中创建用户和团队条目,但失败了。
代码如下。
团队控制器.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
团队.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
团队.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 %>
架构.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 团队中保存数据。