5

I have a Post and a Tag model with a many-to-many association:

post.rb:

class Post < ActiveRecord::Base
  attr_accessible :title, :content, :tag_names

  has_many :taggings, :dependent => :destroy
  has_many :tags, :through => :taggings

  attr_writer :tag_names
  after_save :assign_tags

  def tag_names
    @tag_names || tags.map(&:name).join(" ")
  end

  private

  def assign_tags
    ntags = []
    @tag_names.to_s.split(" ").each do |name|
      ntags << Tag.find_or_create_by_name(name)
    end
    self.tags = ntags
  end
end

tag.rb:

class Tag < ActiveRecord::Base
  has_many :taggings, :dependent => :destroy  
  has_many :posts, :through => :taggings
  has_many :subscriptions
  has_many :subscribed_users, :source => :user, :through => :subscriptions
end

tagging.rb (model for the join table):

class Tagging < ActiveRecord::Base
  belongs_to :post  
  belongs_to :tag
end

I want to create a :counter_cache that tracks how many posts a tag has.

How can I accomplish that in this many-to-many association?

EDIT:

I did this before:

comment.rb:

belongs_to :post, :counter_cache => true

But now that there isn't a belongs_to in the post.rb file. I'm a bit confused.

alexchenco
  • 53,565
  • 76
  • 241
  • 413

2 Answers2

8

tagging.rb (model for the join table):

class Tagging < ActiveRecord::Base
  belongs_to :post,  counter_cache: :tags_count
  belongs_to :tag,  counter_cache: :posts_count
end

add column migration posts_count integer in tags add column migration tags_count integer in posts

zetanova
  • 481
  • 4
  • 11
2

It seems like there is no real easy way to do this. If you look at this previous post. It seems like this comes up fairly often and sadly rails does not have an easy way to complete this task. What you will need to do is write some code like this.

Although, I would also suggest looking into has_and_belongs_to_many relationships as that might be what you have here.

A(Tag) has many C(posts) through B(tagging)

class C < ActiveRecord::Base
    belongs_to :B

    after_create :increment_A_counter_cache
    after_destroy :decrement_A_counter_cache

    private

    def increment_A_counter_cache
        A.increment_counter( 'c_count', self.B.A.id )
    end

    def decrement_A_counter_cache
        A.decrement_counter( 'c_count', self.B.A.id )
    end
end
Community
  • 1
  • 1
Justin Herrick
  • 2,981
  • 17
  • 18
  • I'm using a `many-to-many` association. Is `has_and_belongs_to_many` more suitable for this situation? Why? – alexchenco Mar 07 '12 at 04:35
  • 1
    I actually wouldn't recommend `has_and_belongs_to_many`. It simplifies things, but without a primary key it makes it a pain to migrate away from if you decide you want to store additional information on the join table, which almost inevitably ends up happening. – lobati Jun 29 '14 at 19:18