【发布时间】:2016-10-12 03:58:26
【问题描述】:
我有以下迁移:
class CreateMothers < ActiveRecord::Migration[5.0]
def change
create_table :mothers do |t|
t.string :name
t.timestamps
end
end
end
和:
class CreateSons < ActiveRecord::Migration[5.0]
def change
create_table :sons do |t|
t.string :name
t.references :mother
t.timestamps
end
end
end
每当我尝试将 Mother_id 字段为空白的 Son 对象保存时,我都会收到错误消息:“Mother must exist”
有没有办法在没有mother_id 字段的情况下保存它?
【问题讨论】:
-
您是否有验证来检查您的
Son模型中是否存在mother_id?如果是这样,您需要删除该验证以将 Son 对象保存为mother_id字段为空白。 -
没有验证。但是当我从 Son 模型中删除 belongs_to :mother 时,它工作得很好。
标签: ruby-on-rails activerecord ruby-on-rails-5