Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails4: Can't modify frozen hash

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?

like image 455
CottonEyeJoe Avatar asked Jan 20 '16 09:01

CottonEyeJoe


1 Answers

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?
like image 93
Andrey Deineko Avatar answered Sep 22 '22 05:09

Andrey Deineko