Fernando Maymone
Fernando Maymone

Reputation: 355

How to do a "where" clause correctly in ruby on rails?

I have an application where I should do a query using where on my database. Everything looks fine. This is my model:

class TalentType < ApplicationRecord
    def talent_abilities
        TalentAbility.where(talent_type: self)
    end
end

And in my console when I do the query, it returns correctly the data BUT the last line of the query, is an ActiveRecord::Relation. The problem is, when in my view, I do a:

<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 %>

My last line comes with:

<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">,

So, in console, I have all the TalentAbility returned, perfectly, but the last element comes with this ActiveRecord stuff. Like this:

[#<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">, ...]> 

How to avoid this last line on my query?

Upvotes: 1

Views: 53

Answers (1)

spickermann
spickermann

Reputation: 107077

Just remove the = from the line in which you iterate the result:

<% current_user.talent.talent_type.talent_abilities.each do |ta| %>
  <div class="form-group no-border bottom-0">
    <%= ta.name %>
  </div>
<% end %>

Upvotes: 5

Related Questions