我对rails非常陌生,并试图让三个模型与has_many through关系一起工作,在连接模型上使用备用类名,并使用collection_select表单向连接模型添加记录。这三个型号如下。我对所有协会都有“accepts_nested_attributes_for”。
用户模型
has_many :match_opponents
has_many :home_opponents, :class_name => 'MatchOpponent', :foreign_key => 'home_opponent_id'
has_many :away_opponents, :class_name => 'MatchOpponent', :foreign_key => 'away_opponent_id'
has_many :matches, :through => :match_opponents
匹配模型
has_many :match_opponents
has_many :home_opponents, :class_name => 'MatchOpponent', :foreign_key => 'home_opponent_id'
has_many :away_opponents, :class_name => 'MatchOpponent', :foreign_key => 'away_opponent_id'
has_many :users, :through => :match_opponents
匹配对手模型
belongs_to :user, :inverse_of => :match_opponents
belongs_to :match, :inverse_of => :match_opponents
我的匹配控制器for new和create:
def new
@match = Match.new
@match.home_opponents.build
@match.away_opponents.build
end
def create
@match = Match.create(match_params)
@home_opponents = @match.home_opponents.create!(params[:match_opponents])
@away_opponents = @match.away_opponents.create!(params[:match_opponents])
if @match.save
redirect_to @match, notice: 'Match was successfully created.'
end
end
我的表格:
= simple_form_for @match, html: { multipart: true } do |f|
= simple_fields_for :home_opponents do |ff|
= ff.collection_select :user_id, User.all, :id, :name, {}, {multiple: true}
= simple_fields_for :away_opponents do |ff|
= ff.collection_select :user_id, User.all, :id, :name, {}, {multiple: true}
每个型号的相关允许参数:
def match_params
params.require(:match).permit(Match::MATCH_ATTRIBUTES)
end
MATCH_ATTRIBUTES = [:match_opponent_ids => [], :user_ids => [], match_opponents_attributes: MatchOpponent::MATCH_OPPONENT_ATTRIBUTES]
def user_params
params.require(:user).permit(User::USER_ATTRIBUTES)
end
USER_ATTRIBUTES = [:match_opponent_ids => [], :match_ids => [], :match_opponents_attributes: MatchOpponent::MATCH_OPPONENT_ATTRIBUTES]
def match_opponent_params
params.require(:match_opponent).permit(MatchOpponent::MATCH_OPPONENT_ATTRIBUTES)
end
MATCH_OPPONENT_ATTRIBUTES = [:id, :home_opponent_id, :away_opponent_id, :match_id, :user_id]
我尝试了很多不同的模型配置,但是无法在连接模型(MatchOpponent)上获得多条记录来保存记录的match_id和user_id。
提前感谢您的帮助!
答案 0 :(得分:0)
首先,您需要了解有关控制器操作的更多信息。 您可以在Rails Guides
中了解相关信息例如:
def create
@match = Match.create(match_params)
@home_opponents = @match.home_opponents.create!(params[:match_opponents])
@away_opponents = @match.away_opponents.create!(params[:match_opponents])
if @match.save
redirect_to @match, notice: 'Match was successfully created.'
end
end
Match.create已经保存了对象,您可以使用if @match.save
再次保存该对象。这应该是:
def create
@match = Match.new(match_params)
if @match.save
#create opponents here
redirect_to @match, notice: 'Match was successfully created.'
end
end
答案 1 :(得分:0)
谢谢!用以下解决了这个问题。需要在User模型上而不是在连接模型(MatchOpponent)上创建备用类:
用户模型
has_many :match_opponents
has_many :matches, :through => :match_opponents
has_many :home_opponents, :through => :match_opponents
has_many :away_opponents, :through => :match_opponents
匹配模型
has_many :match_opponents
has_many :home_opponents, :through => :match_opponents
has_many :away_opponents, :through => :match_opponents
匹配对手模型
belongs_to :home_opponent, :class_name => 'User', :foreign_key => 'home_opponent_id'
belongs_to :away_opponent, :class_name => 'User', :foreign_key => 'away_opponent_id'
belongs_to :match, :inverse_of => :match_opponents
以上所有都有accepts_nested_attributes_for。
我的匹配控制器for new和create:
def new
@match = Match.new
end
def create
@match = Match.new(match_params)
@match.update!(:status => 'pending')
if @match.save
@match.match_opponents.where.not(:home_opponent_id => 'nil').each do |o| o.update!(:user_id => o.home_opponent_id) end
@match.match_opponents.where.not(:away_opponent_id => 'nil').each do |o| o.update!(:user_id => o.away_opponent_id) end
redirect_to @match, notice: 'Match was successfully created.'
end
end
我的表格:
= f.collection_select :home_opponent_ids, User.order(:first_name), :id, :name, {}, {multiple: true}
= f.collection_select :away_opponent_ids, User.order(:first_name), :id, :name, {}, {multiple: true}