I have 2 model with the following details:
# Device model
module Rubywebit::Devices
class SensorValue < ApplicationRecord
belongs_to :device, class_name: Rubywebit::Devices::Device
end
end
# SensorValue model
module Rubywebit::Devices
class Device < ApplicationRecord
has_many :sensor_values, class_name: Rubywebit::Devices::SensorValue
end
end
I am using Reform gem (https://github.com/apotonick/reform) to create form objects. My form classes for 2 model is the following code:
# Device form
class DeviceForm < Reform::Form
property :name
property :imei
collection :sensor_values, form: SensorValueForm
end
# SensorValue form
class SensorValueForm < Reform::Form
property :index
property :value
property :date_time
end
As you see I have 2 nested form for 2 model. In step 1 one record insert in to table with references:
df=DeviceForm.new(Rubywebit::Devices::Device.new, sensor_values: [Rubywebit::Devices::SensorValue.new])
df.validate(name: 'A',imei:'B',sensor_values: [index: 1, value: 2])
df.save
output is:
(0.6ms) BEGIN
SQL (0.5ms) INSERT INTO "rubywebit_devices_devices" ("name", "imei", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETUR
NING "id" [["name", "A"], ["imei", "B"], ["created_at", 2016-06-14 05:04:58 UTC], ["updated_at", 2016-06-14 05:04:58 UTC]]
SQL (0.5ms) INSERT INTO "rubywebit_devices_sensor_values" ("index", "value", "device_id", "created_at", "updated_at") VALUES (
$1, $2, $3, $4, $5) RETURNING "id" [["index", 1], ["value", 2.0], ["device_id", 2], ["created_at", 2016-06-14 05:04:58 UTC], ["u
pdated_at", 2016-06-14 05:04:58 UTC]]
(0.8ms) COMMIT
(0.1ms) BEGIN
(0.1ms) COMMIT
and this is OK.
And in step 2 we have a record for device and I want to insert a record to sensor_values table with the device_id that we have:
df=DeviceForm.new(Rubywebit::Devices::Device.find(1), sensor_values: [Rubywebit::Devices::SensorValue.new])
df.validate(sensor_values: [index: 2, value: 3])
df.save
output:
Rubywebit::Devices::SensorValue Load (0.4ms) SELECT "rubywebit_devices_sensor_values".* FROM "rubywebit_devices_sensor_values"
WHERE "rubywebit_devices_sensor_values"."device_id" = $1 [["device_id", 1]]
(0.2ms) BEGIN
SQL (13.1ms) UPDATE "rubywebit_devices_sensor_values" SET "device_id" = NULL WHERE "rubywebit_devices_sensor_values"."device_i
d" = $1 AND "rubywebit_devices_sensor_values"."id" = 17 [["device_id", 1]]
SQL (0.5ms) INSERT INTO "rubywebit_devices_sensor_values" ("index", "value", "device_id", "created_at", "updated_at") VALUES (
$1, $2, $3, $4, $5) RETURNING "id" [["index", 1], ["value", 2.0], ["device_id", 1], ["created_at", 2016-06-14 05:19:51 UTC], ["u
pdated_at", 2016-06-14 05:19:51 UTC]]
(0.6ms) COMMIT
(0.1ms) BEGIN
(0.1ms) COMMIT
(0.1ms) BEGIN
(0.1ms) COMMIT
As you see, Reform does an additional transaction (UPDATE "rubywebit_devices_sensor_values" ...
) why?
I solved this problem by Populate_if_empty feature.