[ACCEPTED]-Understanding Rails ActiveRecord "single model" self joins-activerecord
Accepted answer
I was missing the has_many foreign key to 6 "parent_post_id". Once its set, the post.replies 5 will reference other Post instances by parent_post_id.
The 4 posts.rb model has the relationship defined:
class Post < ActiveRecord::Base
has_many :replies, :class_name => "Post",
:foreign_key => "parent_post_id"
belongs_to :parent_post, :class_name => "Post",
:foreign_key => "parent_post_id"
end
I 3 can now create Posts, assign a parent_post_id 2 to a different Post, then later get all 1 Posts that are replies to any parent Post.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.