我有一个应用程序,应该使用数据库中的where进行查询。一切看起来都很好。这是我的模型:
class TalentType < ApplicationRecord
def talent_abilities
TalentAbility.where(talent_type: self)
end
end
在我的控制台中,当我执行查询时,它会正确返回数据,但查询的最后一行是ActiveRecord :: Relation。问题是,在我看来,我会执行以下操作:
<h6 class="subheading">Abilities</h6>
<%= current_user.talent.talent_type.talent_abilities.each do |ta| %>
<div class="form-group no-border bottom-0">
<%= ta.name %>
</div>
<% end %>
我的最后一行是:
<ActiveRecord::Relation [#<TalentAbility id: 1, name: "Scuba diving", requires_description: false, talent_type_id: 1, created_at: "2018-11-21 15:10:02", updated_at: "2018-11-21 15:10:02">, #<TalentAbility id: 2, name: "River rafting", requires_description: false, talent_type_id: 1, created_at: "2018-11-21 15:10:02", updated_at: "2018-11-21 15:10:02">,
因此,在控制台中,我完美地返回了所有TalentAbility,但是最后一个元素随ActiveRecord一起出现。像这样:
[#<TalentAbility:0x00000005cdab68
id: 1,
name: "Scuba diving",
requires_description: false,
talent_type_id: 1,
created_at: Wed, 21 Nov 2018 15:10:02 UTC +00:00,
updated_at: Wed, 21 Nov 2018 15:10:02 UTC +00:00>,
.........
#<TalentAbility:0x00000005cd03e8
id: 52,
name: "Play Musical Instrument",
requires_description: false,
talent_type_id: 1,
created_at: Wed, 21 Nov 2018 15:10:02 UTC +00:00,
updated_at: Wed, 21 Nov 2018 15:10:02 UTC +00:00>]
=> #<ActiveRecord::Relation [#<TalentAbility id: 1, name: "Scuba diving", requires_description: false, talent_type_id: 1, created_at: "2018-11-21 15:10:02", updated_at: "2018-11-21 15:10:02">, #<TalentAbility id: 2, name: "River rafting", requires_description: false, talent_type_id: 1, created_at: "2018-11-21 15:10:02", updated_at: "2018-11-21 15:10:02">, #<TalentAbility id: 3, name: "Bungee jumping", requires_description: false, talent_type_id: 1, created_at: "2018-11-21 15:10:02", updated_at: "2018-11-21 15:10:02">, #<TalentAbility id: 4, name: "Extreme Sports", requires_description: false, talent_type_id: 1, created_at: "2018-11-21 15:10:02", updated_at: "2018-11-21 15:10:02">, #<TalentAbility id: 5, name: "Basketball", requires_description: false, talent_type_id: 1, created_at: "2018-11-21 15:10:02", updated_at: "2018-11-21 15:10:02">, #<TalentAbility id: 6, name: "Skiing", requires_description: false, talent_type_id: 1, created_at: "2018-11-21 15:10:02", updated_at: "2018-11-21 15:10:02">, #<TalentAbility id: 7, name: "Hiking", requires_description: false, talent_type_id: 1, created_at: "2018-11-21 15:10:02", updated_at: "2018-11-21 15:10:02">, #<TalentAbility id: 8, name: "Ice skating", requires_description: false, talent_type_id: 1, created_at: "2018-11-21 15:10:02", updated_at: "2018-11-21 15:10:02">, #<TalentAbility id: 9, name: "Surfing", requires_description: false, talent_type_id: 1, created_at: "2018-11-21 15:10:02", updated_at: "2018-11-21 15:10:02">, #<TalentAbility id: 10, name: "Racing", requires_description: false, talent_type_id: 1, created_at: "2018-11-21 15:10:02", updated_at: "2018-11-21 15:10:02">, ...]>
如何避免查询的最后一行?
答案 0 :(得分:5)
只需从迭代结果的行中删除=
:
<% current_user.talent.talent_type.talent_abilities.each do |ta| %>
<div class="form-group no-border bottom-0">
<%= ta.name %>
</div>
<% end %>