【发布时间】:2016-09-29 07:12:48
【问题描述】:
我在 Rails 4.2.3 中创建了一个应用程序 我正在尝试为数据库播种,但是在执行 rake db:migrate 之后,我在数据库表的字段中得到了 nil 值。我的seeds.rb 中唯一的一段代码是:
User.create![{username: "test", password_digest: "test"}]
在我的 schema.rb 我有:
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
我的用户模型是:
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy
has_many :todo_lists, dependent: :destroy
has_many :todo_items, through: :todo_lists, source: :todo_items
end
还有其他人遇到过同样的问题,例如 here 和 here,但我认为这些解决方案不适用于我的情况,因为我的任何模型中都没有任何 attr_accessor。
有人知道为什么会这样吗?
【问题讨论】:
-
大括号类型很重要...改用
User.create!( username: "test", password_digest: "test" )
标签: ruby-on-rails ruby-on-rails-4