Ruby-on-rails 未知属性:user_id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11529405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:42:33  来源:igfitidea点击:

unknown attribute: user_id

ruby-on-railsrubyactiverecord

提问by cnaize

I'm getting the error unknown attribute: user_id durring execution of current_user.stories.build

我在 current_user.stories.build 的执行过程中收到错误未知属性:user_id

class User < ActiveRecord::Base
  has_many :stories, class_name: 'Story', foreign_key: 'user_id', dependent: :destroy
  ...

class Story < ActiveRecord::Base
  belongs_to :user, class_name: 'User', foreign_key: 'user_id'
  ...

schema.rb

模式文件

create_table "stories", :force => true do |t|
    t.string   "responsible"
    t.string   "descr"
    t.string   "state"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

  create_table "users", :force => true do |t|
    t.string   "email"
    t.string   "password_digest"
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
    t.string   "name"
  end

It doesn't contain 'user_id' field. Any ideas?

它不包含“user_id”字段。有任何想法吗?

回答by Emily

Kulbir is correct that you need to define a user_idcolumn in your storiestable, but doesn't explain the way to do that.

Kulbir 是正确的,您需要user_idstories表中定义一列,但没有解释这样做的方法。

The correct way to make that change is to create a new migration. By convention, it should be called add_user_id_to_storiesand would be created as follows (assuming you're using Rails 3+):

进行这种更改的正确方法是创建一个新的迁移。按照惯例,应该add_user_id_to_stories按如下方式调用和创建它(假设您使用的是 Rails 3+):

rails generate migration add_user_id_to_stories

If you run that, it should actually generate a migration that already contains the change you need to make, which should be something like:

如果你运行它,它实际上应该生成一个已经包含你需要进行的更改的迁移,它应该是这样的:

add_column :stories, :user_id, :integer

As an aside when you're following the Rails conventions concerning association naming, which you are, you can actually skip a lot of the extra specification. In the Usermodel, you can specify just has_many :storiesand in the Storymodel specify belongs_to :user. Rails will assume the same class names and foreign keys you've specified.

顺便说一句,当您遵循有关关联命名的 Rails 约定时,您实际上可以跳过很多额外的规范。在User模型中,您可以指定 justhas_many :stories并在Story模型中指定belongs_to :user。Rails 将假定您指定的类名和外键相同。

回答by Kulbir Saini

You should have a user_idfield in your storiestable like below to define the association in your models.

user_idstories表格中应该有一个如下所示的字段来定义模型中的关联。

create_table "stories", :force => true do |t|
    t.integer  "user_id"
    t.string   "responsible"
    t.string   "descr"
    t.string   "state"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end
  ...
end
create_table "stories", :force => true do |t|
    t.integer  "user_id"
    t.string   "responsible"
    t.string   "descr"
    t.string   "state"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end
  ...
end

Edit

编辑

Check Emily's answer for detailed explanation.

查看 Emily 的回答以获取详细说明。

回答by RailsZilla.com

you should use the new syntax and pass the fieldtype as symbol

您应该使用新语法并将字段类型作为符号传递

add_column :stories, :user_id, :integer