Topic: has_many tags in nested resources setup

I implemented the tagging feature described at railscasts.

http://railscasts.com/episodes/382-tagging

part of my tag model:

class Tag < ActiveRecord::Base
  has_many :taggings
  has_many :houses, through: :taggings
end
part of my house model

class House < ActiveRecord::Base
    has_many :taggings
    has_many :tags, through: :taggings

   def self.tagged_with(name)
   Tag.find_by_name!(name).houses
end

  def self.tag_counts
    Tag.select("tags.*, count(taggings.tag_id) as count").
      joins(:taggings).group("taggings.tag_id")
  end

  def tag_list
    tags.map(&:name).join(", ")
  end

  def tag_list=(names)
    self.tags = names.split(",").map do |n|
      Tag.where(name: n.strip).first_or_create!
    end
  end
end
part of my house controller:

def index
    @country = Country.find(params[:country_id])
    @regions = @country.regions

    if params[:tag]
      @houses = House.tagged_with(params[:tag])
     else
      @houses = Find(:all)
end
house index view:

.span9
  #container
    - @houses.each do |house|
      .item{:class => house.features_to_html_class }
        %article.chat.t_xs
          .article-base
            %section
              .span4
                %h2 #{link_to house.name, house_path(house)}
                %p
When i visit house/tag/tagname

i get the correct houses who are tagged with current tagname. So this works fine.

But because i implemented a new nested resources setup (locale/country/region/houses) instead (/locale/houses) my view houses index changed in this:

#container
    - @regions.each do |region|
      - region.houses.find_all do |house|
        .item{:class => house.features_to_html_class }
          %article.chat.t_xs
            .article-base
              %section
                .row
                  .span4
                    %h2 #{link_to house.name, country_region_house_path(@country, region, house)}
                    %p
But how i implement the tag feature in the new nested resources setup. I thought changing the "find_all" method in this:

- @regions.each do |region|
     - region.houses.tagged_with(params[:tag]) do |house|
This is not working...can someone advice/help me.

Thanks..remco

Last edited by remco (2013-04-20 05:34:25)