当我尝试在 Rails 中添加占位符关联记录时,嵌套字段表单出现错误 5
Nested Fields form is getting an error when I try to add placeholder association record in Rails 5
我有两个 Rails 型号:workout
、measurable
。当我去我的表格编辑锻炼时,我收到以下错误。
访问锻炼时出错#edit
*** ActiveRecord::NotNullViolation Exception: TinyTds::Error: Cannot insert the value NULL into column 'value', table 'bane-development.dbo.measurables'; column does not allow nulls. INSERT fails.: EXEC sp_executesql N'INSERT INTO [measurables] ([measurable_type_id], [workout_id], [created_at], [updated_at], [import_key]) OUTPUT INSERTED.[id] VALUES (@0, @1, @2, @3, @4)', N'@0 int, @1 int, @2 datetime, @3 datetime, @4 int', @0 = 3, @1 = 689349, @2 = '05-29-2018 09:22:45.206', @3 = '05-29-2018 09:22:45.206', @4 = -1
我想做的是填充我的 Rails workout
表单,让用户可以选择填写各种可衡量指标以添加到锻炼中。但是,如果它们没有值,我不希望它们保存到数据库中。但是当下面的行执行 self.measurables << Measurable.new(measurable_type_id: measurable_type.id, order_by: 1, import_key: -1)
- 它似乎正在尝试保存到数据库,而我还不想保存它......我只是想让它给用户字段以输入数据,如果他们选择的话。在我更新 workout
对象之前,如何才能将关联添加到表单但不保存记录?
workout.rb
class Workout < ApplicationRecord
before_validation :remove_blank_measurables
has_many :measurables, inverse_of: :workout
accepts_nested_attributes_for :measurables, :allow_destroy => true, reject_if: proc {|m| m[:value].blank?}
def add_missing_measurable_types
MeasurableType.workout_observations.each do |measurable_type|
if !self.has_measurable_type?(measurable_type)
self.measurables << Measurable.new(measurable_type_id: measurable_type.id, order_by: 1, import_key: -1)
end
end
end
end
measurable.rb
class Measurable < ApplicationRecord
belongs_to :workout
end
锻炼#edit
def edit
@workout.add_missing_measurable_types
end
collection<<(object, …)
Adds one or more objects to the collection by creating associations in the join table (collection.push and collection.concat are aliases to this method). Note that this operation instantly fires update SQL without waiting for the save or update call on the parent object, unless the parent object is a new record.
'<<'(铲子操作员)开火 update/save,您应该改用
self.measurables.new(measurable_type_id: measurable_type.id, order_by: 1, import_key: -1)
我有两个 Rails 型号:workout
、measurable
。当我去我的表格编辑锻炼时,我收到以下错误。
访问锻炼时出错#edit
*** ActiveRecord::NotNullViolation Exception: TinyTds::Error: Cannot insert the value NULL into column 'value', table 'bane-development.dbo.measurables'; column does not allow nulls. INSERT fails.: EXEC sp_executesql N'INSERT INTO [measurables] ([measurable_type_id], [workout_id], [created_at], [updated_at], [import_key]) OUTPUT INSERTED.[id] VALUES (@0, @1, @2, @3, @4)', N'@0 int, @1 int, @2 datetime, @3 datetime, @4 int', @0 = 3, @1 = 689349, @2 = '05-29-2018 09:22:45.206', @3 = '05-29-2018 09:22:45.206', @4 = -1
我想做的是填充我的 Rails workout
表单,让用户可以选择填写各种可衡量指标以添加到锻炼中。但是,如果它们没有值,我不希望它们保存到数据库中。但是当下面的行执行 self.measurables << Measurable.new(measurable_type_id: measurable_type.id, order_by: 1, import_key: -1)
- 它似乎正在尝试保存到数据库,而我还不想保存它......我只是想让它给用户字段以输入数据,如果他们选择的话。在我更新 workout
对象之前,如何才能将关联添加到表单但不保存记录?
workout.rb
class Workout < ApplicationRecord
before_validation :remove_blank_measurables
has_many :measurables, inverse_of: :workout
accepts_nested_attributes_for :measurables, :allow_destroy => true, reject_if: proc {|m| m[:value].blank?}
def add_missing_measurable_types
MeasurableType.workout_observations.each do |measurable_type|
if !self.has_measurable_type?(measurable_type)
self.measurables << Measurable.new(measurable_type_id: measurable_type.id, order_by: 1, import_key: -1)
end
end
end
end
measurable.rb
class Measurable < ApplicationRecord
belongs_to :workout
end
锻炼#edit
def edit
@workout.add_missing_measurable_types
end
collection<<(object, …) Adds one or more objects to the collection by creating associations in the join table (collection.push and collection.concat are aliases to this method). Note that this operation instantly fires update SQL without waiting for the save or update call on the parent object, unless the parent object is a new record.
'<<'(铲子操作员)开火 update/save,您应该改用
self.measurables.new(measurable_type_id: measurable_type.id, order_by: 1, import_key: -1)