2016-01-20 14 views
7

मूल मॉडल आदेश है_मनी बिक्री संशोधित नहीं कर सकता। जब मैं एक आदेश को नष्ट करना चाहते हैं तो संबद्ध बिक्री भी नष्ट कर दिया जाना चाहिए करने के लिए, लेकिन मैं हमेशा ऐसा करते हुए एक त्रुटि का सामना किया:रेल 4: जमे हुए हैश

RuntimeError in OrdersController#destroy 
Can't modify frozen hash 

इस लाइन मिलता है पर प्रकाश डाला: self.price = mrr * self.quantity.to_f

सभी संबंधित बिक्री रिकॉर्ड मैन्युअल रूप से चरण-दर-चरण त्रुटियों के बिना काम को नष्ट करना। बिक्री के बाद अब कोई बिक्री जुड़ी नहीं है, मैं आदेश रिकॉर्ड को नष्ट करने में सक्षम हूं।

बिक्री मॉडल:

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 

आदेश मॉडल:

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

किसी भी विचार? अग्रिम में धन्यवाद!

उत्तर

12

लाइन आप जब अपने price विशेषता को अद्यतन करने कि sale नष्ट नहीं कर रहा है यह सुनिश्चित करना चाहिये पर प्रकाश डाला जाता है कि पर:

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? 
+0

आपको बहुत बहुत धन्यवाद! यह काम करता है! – CottonEyeJoe

संबंधित मुद्दे