【发布时间】:2014-08-09 03:01:27
【问题描述】:
我有这 3 个课程:
-
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结束
-
WatchedStory:class WatchedStory < ActiveRecord::Base belongs_to :user belongs_to :story, class_name: 'UserStory' end
当我尝试通过UserStory#watchers 列出所有观察者时,我看到了这个错误:
PG::UndefinedColumn: ERROR: column watched_stories.user_story_id does not exist
似乎关系has_many through 是错误的,但我可以看到错误。我在这里想念什么?
我的迁移:
class CreateWatchedStories < ActiveRecord::Migration
def change
create_table :watched_stories do |t|
t.references :user, index: true
t.references :story, index: true, references: :user_story
t.timestamps
end
end
end
【问题讨论】:
-
同样的事情:
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column watched_stories.user_story_id does not exist -
嗯..因为你没有在migration中定义它..这是我们经常犯的错误......
-
它正在watched_stories 表中寻找一个名为
user_story_id的键,但找不到它。您用于将 WatchedStory 连接到 UserStory 的外键名称是什么? -
@ArupRakshit 那么我应该怎么做呢?
-
@Sharagoz 它是
story_id
标签: ruby-on-rails ruby ruby-on-rails-4 has-many-through