so I got confused about AutosaveAssociation
so lets say I have this class
class Bill < ApplicationRecord
has_many :bill_entries, dependent: :destroy
accepts_nested_attributes_for :bill_entries, reject_if: :all_blank, allow_destroy: true
end
class BillEntry < ApplicationRecord
belongs_to :bill
before_validation :set_amount
def set_amount
self.amount = self.price * self.quantity * self.bill.currency_rate
end
end
schema.rb
ActiveRecord::Schema[7.0].define(version: 2022_03_10_061810) do
create_table "bills", force: :cascade do |t|
t.bigint "company_id"
t.decimal "vat", default: "0.0", null: false
t.decimal "currency_rate", default: "0.0", null: false
end
create_table "bill_entries", force: :cascade do |t|
t.bigint "bill_id", null: false
t.decimal "quantity"
t.decimal "price", default: "0.0", null: false
t.decimal "amount", null: false
t.index ["bill_id"], name: "index_bill_entries_on_bill_id"
end
end
why whenever I save or edit the parents the child validation is not called?
even though I have add autosave: true
to the parent like this
class Bill < ApplicationRecord
has_many :bill_entries, dependent: :destroy, autosave: true
accepts_nested_attributes_for :bill_entries, reject_if: :all_blank, allow_destroy: true
end
and when I put the autosave: true
to the child like this instead the parent the validation is called
class BillEntry < ApplicationRecord
belongs_to :bill, autosave: true
before_validation :set_amount
def set_amount
self.amount = self.price * self.quantity * self.bill.currency_rate
end
end
what is the difference between them? I got confused the doc in options for belongs_to and options for has_many have the same words for them but it gives difference behavior