What is wrong with this before_save-callback ?
before_save-callback
class Order < ActiveRecord::Base has_many :line_items, :dependent => :destroy, :inverse_of => :order accepts_nested_attributes_for :line_items attr_accessible :line_items_attributes before_save :mark_line_items_for_removal def mark_line_items_for_removal line_items.each do |line_item| line_item.mark_for_destruction if line_item.quantity.to_f <= 0 end end end
When one of the line_items marked for destruction, no line_item will be saved. However, the parent order object is saved. Returning true doesn't matter ...
line_items
line_item
about mark_for_destruction: http://apidock.com/rails/v3.1.0/ActiveRecord/AutosaveAssociation/mark_for_destruction and why is it instead of ": allow_destroy => true"? take a look here: http://weblogs.manas.com.ar/spalladino/2010/03/15/deleting-children-with-accepts_nested_attributes_for-in-rails/
I believe you need to set: autosave => true option to define has_many.
As indicated here:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many
"If true, always save related objects or destroy them if they are marked for destruction when you save the parent object. If false, never save or destroy related objects. By default, save only related objects that are new entries."
Source: https://habr.com/ru/post/1380857/More articles:TDD has_many through model validation with RSpec & Factory Girl - ruby-on-railsWhy can't we just download the Javadocs SWT jar file from the main website - eclipseCan user literals function as arguments? - c ++iOS 5 - presentModalViewController nils parentViewController in self? - objective-cConstructor in implementation versus header - c ++Why shouldn't I put "use the std namespace" in the header? - c ++Error with re.split function and re.DOTALL flag in python 2.7.1 module - pythonSynchronized scrolling between two instances of NSScrollView - objective-cFirst, Android mobile phone numbers are displayed - androidHow to scale image to frame? - objective-cAll Articles