【问题标题】:ActiveRecord::unknownAttributeError at seeding the data播种数据时的 ActiveRecord::unknownAttributeError
【发布时间】:2016-08-22 07:30:06
【问题描述】:

当我尝试播种我的应用程序时会弹出此错误。

链接到我的repo

我所做的是: - 创建了一个 Rails 应用程序 - 创建了架构中描述的四个模型

ActiveRecord::Schema.define(version: 20160823102902) do

  create_table "profiles", force: :cascade do |t|
    t.string   "gender"
    t.integer  "birth_year"
    t.string   "first_name"
    t.string   "last_name"
    t.integer  "User_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "todo_items", force: :cascade do |t|
    t.date     "due_date"
    t.string   "title"
    t.text     "description"
    t.boolean  "completed",   default: false
    t.integer  "TodoList_id"
    t.datetime "created_at",                  null: false
    t.datetime "updated_at",                  null: false
  end

  add_index "todo_items", ["TodoList_id"], name: "index_todo_items_on_TodoList_id"

  create_table "todo_lists", force: :cascade do |t|
    t.string   "list_name"
    t.date     "list_due_date"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
    t.integer  "User_id"
  end

  add_index "todo_lists", ["User_id"], name: "index_todo_lists_on_User_id"

  create_table "users", force: :cascade do |t|
    t.string   "username"
    t.string   "password_digest"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
  end

end

并且我创建的四个模型之间有一些关系,如模型文件中所述

class User < ActiveRecord::Base
    has_one :profile , dependent: :destroy
    has_many :todo_lists
    has_many :todo_items,through: :todo_lists,source: :todo_items
end

class Profile < ActiveRecord::Base
    belongs_to :user
end

class TodoList < ActiveRecord::Base
    belongs_to :user
    has_many :todo_items , dependent: :destroy
end

class TodoItem < ActiveRecord::Base
  belongs_to :todo_list
end

当我在seed.rb文件中添加一些数据时,错误开始出现,如图所示

User.destroy_all

User.create! [
    {username: "Fiorina" , password_digest: "123"},
    {username: "Trump"   , password_digest: "456"},
    {username: "Carson"  , password_digest: "789"},
    {username: "Clinton" , password_digest: "abc"},     
]
User.find_by!(username: "Fiorina").create_profile(first_name: "Carly"  , last_name: "Fiorina" )
User.find_by!(username: "Trump"  ).create_profile(first_name: "Donald" , last_name: "Trump"   )
User.find_by!(username: "Carson" ).create_profile(first_name: "Ben"    , last_name: "Carson"  )
User.find_by!(username: "Clinton").create_profile(first_name: "Hillary", last_name: "Clinton" ) 

谁能向我解释这个错误是什么意思,或者我应该做什么?

【问题讨论】:

    标签: ruby-on-rails ruby database activerecord rails-activerecord


    【解决方案1】:

    问题在于profiles 表中的列名。它是User_id,但实际上应该是user_id(约定优于配置)

    您可以通过重命名列来解决这个问题(profilestodo_lists

    rails g migration rename_user_id
    

    这将在您的db/migrate 文件夹中生成一个新的迁移文件。通过将以下内容添加到 change 方法来编辑迁移文件。

    rename_column :profiles, :User_id, :user_id
    

    您还想重命名todo_lists 表中的User_id 列,例如

    rename_column :todo_lists, :User_id, :user_id
    

    【讨论】:

    • 没用 :( ,但现在我知道问题出在哪里了,这可以通过重新创建模型来解决吗?
    • 您不必重新创建模型。你在编辑迁移后运行rake db:migrate 了吗?如果是,您现在遇到什么错误?这是我确定的解决方法。 Rails 正在寻找 user_id 列,但您已将其大写。因此,重命名该字段显然应该有效。
    • 是的,我也确定这是问题所在,但错误没有改变,而且架构也没有改变,虽然它现在指向我们的迁移,也许我已经添加了线错了
    • 类 RenameUserId def change rename_column :profiles, :User_id, :user_id rename_column :todo_lists, :User_id, :user_id end end
    • rake db:migrate 运行时是否没有任何错误?如果是,您应该看到重命名完成。打开 rails 控制台并查看 column_names 是否已更改。在控制台中,键入 Profile.column_names。它将返回profiles 表中的列名数组。看看它是否改变了。
    猜你喜欢
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多