更新rails中的多对多关联(用户 - 团队关系又称成员身份)

时间:2014-08-01 14:59:10

标签: ruby-on-rails ruby ruby-on-rails-3 controller associations

我刚刚创建了一个创建功能,用于添加团队并将成员添加到所述团队。

表格包含以下内容:

  1. 团队名称。
  2. 团队的部门。
  3. Leader(值是用户ID,但显示为全名)
  4. 成员(值是用户ID,但也显示为全名)//我使用了一个名为select2的特殊选择菜单。
  5. 以下是模型。我只会展示与我的问题相关的关联和一些方法。

    user.rb

    class User < ActiveRecord::Base
        has_many :memberships
        has_many :teams, through: :memberships
    
        accepts_nested_attributes_for :memberships, :teams
    end
    

    team.rb

    class Team < ActiveRecord::Base
        has_many :memberships
        has_many :teams, through: :memberships
    
        accepts_nested_attributes_for :memberships, :users
    
        def build_membership(user_ids)
            unless user_ids.blank?
                user_ids.each do |id|   
                    self.users << User.find_by_id(id)
                end
            end
        end
    
    end
    

    membership.rb

    class Membership < ActiveRecord::Base
        belongs_to :team
        belongs_to :user
    
        #Note that leader and members are both users
    end
    

    这是控制器,带有创建和更新方法。

    class TeamsController < ApplicationController
        def create
            @team = Team.new(team_params)
            @team.build_membership(build_members_array(members_params))
    
            if @team.save
                flash.now[:success] = 'Team was successfully created.'
                redirect_to @team
            else
                flash.now[:notice] = @team.errors.full_messages
                render "new"
            end
        end
    
        def update
            # TO-DO: update leader and members (i.e. add or remove member)
            if @team.update(team_params)
                flash.now[:success] = "Team was successfully updated."
                redirect_to @team
            else
                flash.now[:notice] = @team.errors.full_messages
                render "edit"
            end
        end
    
        private
            def team_params
                params.require(:team).permit(:name,:department)
            end
    
            def members_params
                params.require(:team).permit(:leader, members:[])
            end
    
            def build_user_ids_array(members)
                #put ids of leader and members in an array
            end
    end
    

    似乎只有名称和部门属性才会更新,而领导者和成员则不会。我是否应该再次创建自己的方法来更新团队名单或做其他事情?

1 个答案:

答案 0 :(得分:0)

@team.build_membership如果您传递参数来创建新用户,但您使用的是现有的User,那么这将不适用。您需要改为通过User获取所有id,然后添加Team。您还需要在id方法的members_attributes中允许member_params。 类似的东西:

def create
    @team = Team.new(team_params)
    @team.memberships << users_from_params

    if @team.save
        flash.now[:success] = 'Team was successfully created.'
        redirect_to @team
    else
        flash.now[:notice] = @team.errors.full_messages
        render "new"
    end
end

def members_params
   params.require(:team).permit(:leader, members_attributes: [:id])
end

def users_from_params
  #This members_params[:members_attributes].values should be an array of `id`s
  User.find(members_params[:members_attributes].values)
end