Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 有很多:通过查找不存在的列_Ruby On Rails_Ruby_Ruby On Rails 4_Has Many Through - Fatal编程技术网

Ruby on rails 有很多:通过查找不存在的列

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

我有这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 :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
    通过
    story\u id
    连接,您需要指定,否则Rails将假定它是
    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
    
    class WatchedStory
    PG::UndefinedColumn:错误:列监视的\u stories.user\u story\u id 不存在

    Rails正在寻找一个名为
    user\u story\u id
    的列,该列在您的
    关注的\u stories
    表中不存在

    原因

    您有一行
    属于:story,class\u name:“UserStory”
    WatchedStory
    模型中,因此在
    class\u name
    指定为
    UserStory
    时,Rails默认情况下将查找
    用户故事id

    修复

    通过在您的
    WatchedStory
    型号中设置
    外键
    选项,可以解决此错误

    class WatchedStory < ActiveRecord::Base
      belongs_to :user
      belongs_to :story, class_name: 'UserStory',foreign_key: :story_id
    end
    
    class WatchedStory
    同样的事情:
    ActiveRecord::Statement无效:PG::UnfinedColumn:错误:column wasted\u stories.user\u story\u id不存在
    Humm。。因为您没有在迁移中定义它。。这是我们经常犯的错误…它正在“关注的故事”表中查找名为
    user\u story\u id
    的键,但找不到它。您用于将WatchedStory连接到UserStory的外键的名称是什么?@ArupRakshit那么我应该怎么做?@Sharagoz it is
    story\u id
    class WatchedStory < ActiveRecord::Base
      belongs_to :user
      belongs_to :story, class_name: 'UserStory',foreign_key: :story_id
    end