문제

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?

도움이 되었습니까?

해결책

Thanks to Thahakp the simple change was to add where to dogs.tags.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top