爲什麼創建方法會創建nil
記錄?Rails創建方法導致無記錄
我曾嘗試:
Dropdown.create(主題: 「測試」)
Dropdown.create({主題: 「測試」,subject_value: 「1」})
Dropdown.create({:subject =>「test」,:subject_value =>「1」})
全部結果nil
記錄。
(0.1ms) begin transaction
SQL (0.6ms) INSERT INTO "dropdowns" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2016-01-21 23:58:04.979225"], ["updated_at", "2016-01-21 23:58:04.979225"]]
(2.1ms) commit transaction
=> #<Dropdown id: 145, subject: nil, visible: nil, subject_value: nil, subject_description: nil, created_at: "2016-01-21 23:58:04", updated_at: "2016-01-21 23:58:04
型號文件
class Dropdown < ActiveRecord::Base
FIELDS = [
:subject,
:visible,
:subject_value,
:subject_description
]
attr_accessor(*FIELDS)
subjects = %w[math english spanish]
subjects.each do |s|
scope s.to_sym, -> { where(subject: s) }
end
end
遷移文件
class CreateDropdowns < ActiveRecord::Migration
def change
create_table :dropdowns do |t|
t.string :subject
t.boolean :visible
t.string :subject_value
t.string :subject_description
t.timestamps null: false
end
end
end
你可能不希望'attr_accessor(* FIELDS)'在你的模型中,你正在尋找ActiveRecord的憤怒 – bjhaid
見https://stackoverflow.com/questions/4700785/using-attr-accessor-and- attr-accessible-on-the-same-field#answer-4735726 and https://stackoverflow.com/questions/2793098/usage-of-attr-accessor-in-rails – bjhaid