我在Rails 4.2.3中創建了一個應用程序 我試圖給數據庫添加種子,但是在做了rake db之後:migrate我在數據庫字段中獲取了nil值表。代碼的唯一的一塊,我有我的seeds.rb是:Rails - 用nil而不是值填充數據庫
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!(用戶名:「test」,password_digest:「test」)'代替 –