Thanks to Thahakp the simple change was to add where
to dogs.tags
.
How can I list certain types of my Tag class?
-
28-09-2022 - |
Question
I'm having trouble listing a Dogs
tags as I want them to be. There's 3 different tag_types
on my Tag
class, which are red, blue and green.
class Tag
has_many :taggings
has_many :dogs, through: :taggings
TAG_TYPES = %w(Red Blue Green)
validates :tag_type, inclusion: { :in => TAG_TYPES }
end
class DogsController
def index
@dogs = Dog.text_search(params[:query]).page(params[:page]).per_page(5)
end
end
I'm searching for a Dog
and it can have all 3 tag types on it. I would like to list the last 3 added tags for each type separately like this:
dogs/index.html.erb
<% @dogs.each do |dog| %>
<% dog.tags(:tag_type => "Red").last(3).each do |tag| %>
<%= tag.name %>
<% end %>
<% dog.tags(:tag_type => "Blue").last(3).each do |tag| %>
<%= tag.name %>
<% end %>
<% dog.tags(:tag_type => "Green").last(3).each do |tag| %>
<%= tag.name %>
<% end %>
<% end %>
I tried this but it list any tag no matter the color so all the results for listing 3 tags are the same. How could I just show the correct ones for each block?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow