ruby on rails - how could I write this more succinctly -
i have rails model called header has bunch of items. both headers , items have attribute called is_enabled , when header's is_enabled set false, want update items. have feels should one-liner. how make shorter?
after_save :update_menu_items def update_items if self.is_enabled_changed? self.items.each |item| if self.is_enabled==true item.is_enabled=true else item.is_enabled=false end item.save! end end end
it appears want item's is_enabled
attribute match parent header's attribute. in case, self.is_enabled
check unnecessary.
def update_items self.items.each{|i| i.update_attributes(is_enabled: self.is_enabled) } if self.is_enabled_changed? end
in case, #update_attributes
triggers save on each of child items updates them.
depending on how , how method called, , how many child items associated, , how expensive method is, may not want check see if enabled attribute changing:
def update_items self.items.each{|i| i.update_attributes(is_enabled: self.is_enabled) } end
Comments
Post a Comment