Rails4: Can't modify frozen hash

CottonEyeJoe picture CottonEyeJoe · Jan 20, 2016 · Viewed 15k times · Source

The Order model:

class Order < ActiveRecord::Base
  has_many :sales, dependent: :destroy, inverse_of: :order
end

has_many Sales:

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 Sales 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 Sales records manually step-by-step works without errors. After no Sale is associated anymore, I can destroy the Order record too.

Any Ideas?

Answer

Andrey Deineko picture Andrey Deineko · Jan 20, 2016

On the line that gets highlighted you should make sure that sale is not destroyed when updating its price attribute:

self.price = mrr * quantity.to_f unless destroyed? # notice, no need for self before quantity
# or
write_attribute(:price, mrr * quantity.to_f) unless destroyed?