Rails 5.0 nested Reform
<p>I have 2 model with the following details:</p>

<pre><code># Device model
module Rubywebit::Devices
  class SensorValue &lt; ApplicationRecord
    belongs_to :device, class_name: Rubywebit::Devices::Device
  end
end

# SensorValue model
module Rubywebit::Devices
  class Device &lt; ApplicationRecord
    has_many :sensor_values, class_name: Rubywebit::Devices::SensorValue
  end
end
</code></pre>

<p>I am using Reform gem (<a href="https://github.com/apotonick/reform" rel="nofollow">https://github.com/apotonick/reform</a>) to create form objects. My form classes for 2 model is the following code:</p>

<pre><code># Device form
class DeviceForm &lt; Reform::Form
  property :name
  property :imei
  collection :sensor_values, form: SensorValueForm
end

# SensorValue form
class SensorValueForm &lt; Reform::Form
  property :index
  property :value
  property :date_time
end
</code></pre>

<p>As you see I have 2 nested form for 2 model. In step 1 one record insert in to table with references:</p>

<pre><code>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
</code></pre>

<p>output is:</p>

<pre><code>(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
</code></pre>

<p>and this is OK.</p>

<p>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:</p>

<pre><code>df=DeviceForm.new(Rubywebit::Devices::Device.find(1), sensor_values: [Rubywebit::Devices::SensorValue.new])
df.validate(sensor_values: [index: 2, value: 3])
df.save
</code></pre>

<p>output: </p>

<pre><code>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
</code></pre>

<p>As you see, Reform does an additional transaction (<code>UPDATE "rubywebit_devices_sensor_values" ...</code>) why?</p>