Rails Associations - has_many => :through - but same model

Viewed 14586

What I am trying to do:

I have a blog and want to show related posts below the main post.

class Post < ActiveRecord::Base

  has_many :related_posts
  has_many :posts, :through => :related_posts

end

And then in the join model/table

class RelatedPost < ActiveRecord::Base

  belongs_to :post

end

And of course there is a table called related_posts with two post_id columns.

Obviously there are several flaws with this, I'm just not sure how to make this association work in Rails.

2 Answers
Related