活动记录关联类型不匹配

Pie*_*rre 3 activerecord ruby-on-rails ruby-on-rails-4

我在这样的匹配上嵌套了结果

/匹配/ 16 /结果/ 13 /编辑

我有以下选择字段,这显示正确的信息(team.name及其team.id)

<%= f.collection_select :winner, @select_winner_loser, :id, :name %>
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试编辑我的结果并选择获胜者时,我得到了这个:

ActiveRecord :: AssociationTypeMismatch 团队(#10504340)预期,得到字符串(#6163240)

"当分配给关联的对象具有不正确的类型时引发." http://api.rubyonrails.org/classes/ActiveRecord/AssociationTypeMismatch.html

Winner是一个团队对象,result.rb看起来像这样

class Result < ActiveRecord::Base
    has_one :match
    belongs_to  :winner, class_name: "Team"
    belongs_to  :loser, class_name: "Team"
end
Run Code Online (Sandbox Code Playgroud)

@select_winner_loser来自我的results_controller

  def edit
    @select_winner_loser = []
    @select_winner_loser << @match.top
    @select_winner_loser << @match.bottom
  end
Run Code Online (Sandbox Code Playgroud)

Match.top和bottom也是团队对象

class Match < ActiveRecord::Base
    belongs_to  :top, class_name: "Team"
    belongs_to  :bottom, class_name: "Team"
    ...
    belongs_to  :result
end
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我会得到这个,因为我在选择字段中有正确的对象,任何想法?

谢谢

lun*_*unr 14

更改

<%= f.collection_select :winner, @select_winner_loser, :id, :name %>
Run Code Online (Sandbox Code Playgroud)

<%= f.collection_select :winner_id, @select_winner_loser, :id, :name %>
Run Code Online (Sandbox Code Playgroud)

和相应的允许参数.当Rails Team看到_id名称时会创建一个对象.