Ruby on rails 将预先存在的记录与预先存在的父对象关联(2个父对象)
我在使用的嵌套属性中有几个模型 我有“团队”(有许多康斯特)和“竞赛”(属于团队)。但我也希望竞赛引用“类别”作为子对象(竞赛只能有一个类别,一个类别可以有多个竞赛) 逻辑工作的方式是首先创建一个团队,然后创建一个竞赛,然后我希望能够从类别列表(部分)中选择并建立关联(将竞赛中的类别id设置为类别中的id值)。当我作为团队的一员创建一个新的竞赛时,这对我来说是有意义的,但是当涉及到创建第二个关系(现有竞赛到现有父类别)时,我的头撞到了墙上 为我提供竞赛显示视图的控制器是:Ruby on rails 将预先存在的记录与预先存在的父对象关联(2个父对象),ruby-on-rails,nested-attributes,associated-object,Ruby On Rails,Nested Attributes,Associated Object,我在使用的嵌套属性中有几个模型 我有“团队”(有许多康斯特)和“竞赛”(属于团队)。但我也希望竞赛引用“类别”作为子对象(竞赛只能有一个类别,一个类别可以有多个竞赛) 逻辑工作的方式是首先创建一个团队,然后创建一个竞赛,然后我希望能够从类别列表(部分)中选择并建立关联(将竞赛中的类别id设置为类别中的id值)。当我作为团队的一员创建一个新的竞赛时,这对我来说是有意义的,但是当涉及到创建第二个关系(现有竞赛到现有父类别)时,我的头撞到了墙上 为我提供竞赛显示视图的控制器是: def show @t
def show
@team = Team.find(params[:team_id])
@contest = Contest.find(params[:id])
@categories = Category.all
respond_to do |format|
format.html # show.html.erb
format.json { render json: [@contest] }
end
结束
在show视图中,我有以下代码:
<p><b>Name:</b><%= @contest.name %></p>
<%= link_to 'Edit', edit_team_contest_path(@team, @contest) %> |
<%= link_to 'Back', team_contests_path %>
<br />
<%= render 'categories/index'%>
<table>
<% @categories.each do |category| %>
<tr>
<td><%= category.level1 %></td>
<td><%= category.level2 %></td>
<td><%= category.level3 %></td>
<td><%= category.level4 %></td>
<td><%= link_to 'Show', category %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, confirm: 'Are you sure?', method: :delete %></td>
<%end%>
</table>
名称:
|
我的分类部分索引包含以下代码:
<p><b>Name:</b><%= @contest.name %></p>
<%= link_to 'Edit', edit_team_contest_path(@team, @contest) %> |
<%= link_to 'Back', team_contests_path %>
<br />
<%= render 'categories/index'%>
<table>
<% @categories.each do |category| %>
<tr>
<td><%= category.level1 %></td>
<td><%= category.level2 %></td>
<td><%= category.level3 %></td>
<td><%= category.level4 %></td>
<td><%= link_to 'Show', category %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, confirm: 'Are you sure?', method: :delete %></td>
<%end%>
</table>
让我感到困惑的是,设置类别竞赛父子关系的代码(在竞赛或类别控制器中?),以及哪个视图(竞赛显示视图,或类别索引部分?)。我很确定我对Rails的一些基本知识还不太了解,所以如果有人能给我指出一些文档,让我澄清我的困惑,我将不胜感激。好的,下面是我解决问题的方法(以防以后有人发现并使用我尝试过的相同搜索词): 型号:
team.rb
has_many :contests, :dependent => :destroy
category.rb
has_many :contests
contest.rb
belongs_to :team, :foreign_key => "team_id"
belongs_to :category, :class_name => 'Category', :foreign_key =>"category_id"
accepts_nested_attributes_for :category
控制器:
contests_controller
def update
@contest = Contest.find(params[:id])
@team = @contest.team
if !params[:category_id].nil?
@category = Category.find(params[:category_id])
@contest.update_attributes(:category_id => @category.id)
end
respond_to do |format|
if @contest.update_attributes(params[:contest])
blah
else
blah
end
end
end
类别视图(_索引)是竞赛/表演视图中的一部分,包括以下三位代码:
<table>
<% @categories.each do |category| %>
<tr>
<td><%= form_for [category, @contest] do |f| %>
<% f.submit "Select" %>
<% end %></td>
</tr>
<%end%>
</table>
这就是将属于另一个父级的记录与不同模型中的另一个父级关联(在创建第一个关系之后)所需要的条件。好的,如果其他人发现这个问题,我就这样解决了。