The Order
model:
class Order < ActiveRecord::Base
has_many :sales, dependent: :destroy, inverse_of: :order
end
has_many Sale
s:
class Sale < ActiveRecord::Base
belongs_to :order, inverse_of: :sales
validates :order, :product, :product_group, :presence => true
before_create :price
def price
mrr = Warehouse.where(:product => self.product).pluck(:mrr).shift.strip.sub(',', '.').to_f
self.price = mrr * self.quantity.to_f
end
end
When I destroy an Order
, the associated Sale
s should also be destroyed, but I ran into an error when doing so:
RuntimeError in OrdersController#destroy
Can't modify frozen hash
This line is highlighted: self.price = mrr * self.quantity.to_f
.
Destroying all associated Sale
s records manually step-by-step works without errors. After no Sale
is associated anymore, I can destroy the Order
record too.
Any Ideas?