has_many through a habtm relationship in Rails

Posted by macek on Stack Overflow See other posts from Stack Overflow or by macek
Published on 2010-05-07T01:33:19Z Indexed on 2010/05/07 1:38 UTC
Read the original article Hit count: 318

I'm trying to define a has_many X, :through => Y where Y is a habtm relationship. Rails is throwing a fit about this. See comment in user model:

class User < ActiveRecord::Base
  has_many :posts

  # I want to display a list of all tags this user is involved in
  has_many :tags, :through => :posts # ERROR
end

class Post < ActiveRecord::Base
  has_and_belongs_to_many :tags
end

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :posts
end

What can I do to fix this?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord