ruby on rails - has_many :through looking for inexistent column -
ruby on rails - has_many :through looking for inexistent column -
i have these 3 classes:
user
:
class user < activerecord::base end
userstory
:
class userstory < activerecord::base belongs_to :owner, class_name: 'user' belongs_to :assigned, class_name: 'user' belongs_to :board has_many :comments has_many :watched_stories has_many :watchers, through: :watched_stories, source: :user
end
watchedstory
:
class watchedstory < activerecord::base belongs_to :user belongs_to :story, class_name: 'userstory' end
when seek list watchers via userstory#watchers
see error:
pg::undefinedcolumn: error: column watched_stories.user_story_id not exist
it seems relation has_many through
wrong, can see error. missing here?
my migration:
class createwatchedstories < activerecord::migration def alter create_table :watched_stories |t| t.references :user, index: true t.references :story, index: true, references: :user_story t.timestamps end end end
if watchedstory
, userstory
connected through story_id
need specify that, otherwise rails assume it's user_story_id
:
class watchedstory < activerecord::base belongs_to :story, class_name: 'userstory', foreign_key: :story_id end class userstory < activerecord::base has_many :watched_stories, foreign_key: :story_id end
ruby-on-rails ruby ruby-on-rails-4 has-many-through
Comments
Post a Comment