Ruby on rails 有很多:通过查找不存在的列
我有这3门课:Ruby on rails 有很多:通过查找不存在的列,ruby-on-rails,ruby,ruby-on-rails-4,has-many-through,Ruby On Rails,Ruby,Ruby On Rails 4,Has Many Through,我有这3门课: 用户: class User < ActiveRecord::Base end 结束 WatchedStory: class UserStory < ActiveRecord::Base belongs_to :owner, class_name: 'User' belongs_to :assigned, class_name: 'User' belongs_to :board has_many :comments has_many :wat
:用户
结束class User < ActiveRecord::Base end
:WatchedStory
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
似乎关系class WatchedStory < ActiveRecord::Base belongs_to :user belongs_to :story, class_name: 'UserStory' end
有很多错误,但我可以看到错误。我错过了什么 我的迁移:
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
如果class CreateWatchedStories
和WatchedStory
通过UserStory
连接,您需要指定,否则Rails将假定它是story\u id
:user\u 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
PG::UndefinedColumn:错误:列监视的\u stories.user\u story\u id 不存在 Rails正在寻找一个名为class WatchedStory
的列,该列在您的user\u story\u id
表中不存在 原因 您有一行关注的\u stories
在属于:story,class\u name:“UserStory”
模型中,因此在WatchedStory
指定为class\u name
时,Rails默认情况下将查找UserStory
修复 通过在您的用户故事id
型号中设置WatchedStory
选项,可以解决此错误外键
class WatchedStory < ActiveRecord::Base belongs_to :user belongs_to :story, class_name: 'UserStory',foreign_key: :story_id end
同样的事情:class WatchedStory
Humm。。因为您没有在迁移中定义它。。这是我们经常犯的错误…它正在“关注的故事”表中查找名为ActiveRecord::Statement无效:PG::UnfinedColumn:错误:column wasted\u stories.user\u story\u id不存在
的键,但找不到它。您用于将WatchedStory连接到UserStory的外键的名称是什么?@ArupRakshit那么我应该怎么做?@Sharagoz it isuser\u story\u id
story\u id
class WatchedStory < ActiveRecord::Base belongs_to :user belongs_to :story, class_name: 'UserStory',foreign_key: :story_id end