Ruby on rails rails在show中有很多只显示表名
我有以下许多联想Ruby on rails rails在show中有很多只显示表名,ruby-on-rails,Ruby On Rails,我有以下许多联想 class Indicator < ApplicationRecord belongs_to :statement, required: false belongs_to :identity, required: false belongs_to :phase, required: false end class Statement < ApplicationRecord belongs_to :student, required: false
class Indicator < ApplicationRecord
belongs_to :statement, required: false
belongs_to :identity, required: false
belongs_to :phase, required: false
end
class Statement < ApplicationRecord
belongs_to :student, required: false
has_many :indicators
has_many :identities, through: :indicators
accepts_nested_attributes_for :identities
end
class Identity < ApplicationRecord
has_many :indicators
has_many :statements, through: :indicators
accepts_nested_attributes_for :statements
has_many :phases
end
class Phase < ApplicationRecord
has_many :indicators
belongs_to :identity, required: false
end
因为它有许多阶段,所以应该用循环显示每个阶段的名称
<% l.phases.each do |p| %>
<%= p.name %>
<% end %>
你的联想是不正确的。由于标识有多个:阶段,且阶段属于:标识,因此没有理由指定直通选项。那你就这样做了
<% @statement.identities.each do |l| %>
<tr>
<td><%= l.description %></td>
<td><%= l.phases.pluck(:name) %></td>
</tr>
<% end %>
检索特定Identity实例的每个阶段的名称 谢谢你,香农。我试过了,但似乎列出了所有相关的标识。我想我的联想可能错了。我已经更新了上面的问题,以显示我的指标表是如何构造的。谢谢你,特卡先生。上面的剧照给了我一个数组/散列。尝试使用pulk:name而不是select,它将返回值而不是实际对象。我不走运。它正在分阶段查找密钥id。没有这样的列:phases.identity\u id:从phases.identity\u id=?。我希望在上面列出的指标表中匹配。我想我已经正确地承认了自己。你到底想用指标模型做什么?如果我没弄错的话,您必须在您的阶段表中设置一个外键。在迁移中使用add_foreign_key:phases,:identifies。这将允许您显示属于identity的所有阶段当您要显示属于identity的所有阶段时,只能通过identity:references完成,换句话说,在阶段模型上具有identity\u id外键引用
<% l.phases.each do |p| %>
<%= p.name %>
<% end %>
<% @statement.identities.each do |l| %>
<tr>
<td><%= l.description %></td>
<td><%= l.phases.pluck(:name) %></td>
</tr>
<% end %>