本周末,我决定采用Rails 4进行旋转,并立即遇到以下问题:
我有两个模型(想要尝试OpenSchema,你想知道):
has_many :ns_attributes
belongs_to :record
现在在控制台中:
record = Record.create!(name: "blowing in the wind")
nsa = NsAttribute.new(key: "artist", value: "bob dylan", record: record)
#<NsAttribute id: nil, key: "artist", value: "bob dylan", record_id: 4, created_at: nil, updated_at: nil>
irb(main):007:0> nsa.save!
(0.4ms) BEGIN
Record Exists (0.7ms) SELECT 1 AS one FROM "records" WHERE "records"."name" IS NULL LIMIT 1
(0.2ms) COMMIT
=> true
irb(main):008:0> nsa
=> #<NsAttribute id: nil, key: "artist", value: "bob dylan", record_id: nil, created_at: nil, updated_at: nil>
如您所见,记录未保存(record_id:nil)。
关于最新情况的任何线索都表示赞赏!
答案 0 :(得分:1)
我遇到了同样的问题。它似乎是Rails 4中的一个错误。以下是测试用例:
https://gist.github.com/jemmyw/8163504
以下是问题:
https://github.com/rails/rails/issues/13522
您可以在每个模型的基础上通过在具有记录关联的模型下添加以下代码段来修复它:
NsAttribute::GeneratedFeatureMethods.module_eval %Q{
def create_record(*args, &block)
super
end
}
答案 1 :(得分:0)
请改为尝试:
record.ns_attributes.create!(key: "artist", value: "bob dylan")