I define a Post class which can link or be linked to multiple posts. To do this I added a PostLink class which specifies post_to and post_from.
I generated the PostLink class by rails g model post_link from_post:integer to_post:integer and of course rake db:migrate, and added
belongs_to :from_post, :class_name => 'Post'
belongs_to :to_post, :class_name => 'Post'
to the class.
And I also have has_many :post_links in my Post class.
I ran rails console and Post.new.post_links and got nil printed out, which is expected. However after I save a Post using
p = Post.new
p.save
and then run p.post_links, it prints out the following error message:
SQLite3::SQLException: no such column: post_links.post_id: SELECT "post_links".*
FROM "post_links" WHERE "post_links"."post_id" = 1
So anybody know why after saving it to the database post_link can not be accessed?
The
has_many :post_linksassociation inPostthrows an error because it assumes the foreign key in post_links ispost_idby default. Since you are usingfrom_post_idandto_post_id, you will have to find a way to group the post_links for “from” posts and “to” posts to get the total set of post_links for a post.One approach could be to define two associations on
Postand an additional method to add the sets together:As another option, you could provide special sql to the association to group the sets in a single query: