Rails - column not found for defined 'has_many' relationship
Posted
by
Guanlun
on Stack Overflow
See other posts from Stack Overflow
or by Guanlun
Published on 2012-10-14T02:42:24Z
Indexed on
2012/10/14
3:37 UTC
Read the original article
Hit count: 88
ruby-on-rails
|database
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?
© Stack Overflow or respective owner