我正在使用rails has_many通过选项而不确定我是否在这里做错了。我想让一个球员创造一个赛季,当一个球员即将创造一个赛季时,它会显示我在多年/新赛季创造的所有赛季的精选菜单。到目前为止,这部分工作得很好但是当玩家尝试拯救赛季时,铁轨并没有保存。我不确定我的关联是否正确或我做错了什么?有什么理由不行吗?
错误
No association found for name `season_year'. Has it been defined yet?
season.rb
class season < ActiveRecord::Base
belongs_to :player
has_many :quarters
has_many :years , :through => :quarters
attr_accessible :season_year_attributes
accepts_nested_attributes_for :season_year
end
quarter.rb
class Quarter < ActiveRecord::Base
belongs_to :player
belongs_to :year
belongs_to :season
end
year.rb
class Year < ActiveRecord::Base
attr_accessible :season_year, :season_name
has_many :quarters
has_many :seasons, :through => :quarters
end
player.rb
class player < ActiveRecord::Base
has_many :seasons, :through => :quarters
has_many :years, :through => :quarters
end
_season-form.html.erb
<%= form_for(@season) do |f| %>
<div class="field">
<%= f.label :season %>
<%= f.text_field :season_name %>
</div>
<%= f.fields_for :years do |year| %>
<%= select("season", "year_ids", Year.all.collect {|p| [ p.season_year, p.id ] }, { :include_blank => true }) %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
答案 0 :(得分:2)
根据您的型号,我相信您需要更改此内容:
accepts_nested_attributes_for :season_year
到此:
accepts_nested_attributes_for :years
当您接受嵌套属性时,它是模型而不是模型的属性(season_year是年模型的属性,而不是接受实际模型的嵌套属性,Year)。
编辑:
在Season模型中,我将year_ids添加到attr_accessible表达式:
attr_accessible :season_year_attributes, :year_ids
我还更改了季节表单,以便多年来选择列表的输出仅 :
<%= select("season", "year_ids", Year.all.collect {|p| [ p.title, p.id ] }, { :include_blank => true }) %>
答案 1 :(得分:1)
你的Season课程中没有任何season_year,这就是答案。
您是否打算将其作为一个协会?