Skip to content

Instantly share code, notes, and snippets.

@gerardvcruz
Created March 12, 2015 07:07
Show Gist options
  • Save gerardvcruz/7dde2b4db922c4549950 to your computer and use it in GitHub Desktop.
Save gerardvcruz/7dde2b4db922c4549950 to your computer and use it in GitHub Desktop.
hey everyone, I'm dealing with multiple child objects under 1 parent object (one to many relationship) right now in rails and I was wondering how you guys will handle the updating the parent object and the multiple child objects. this is what i'm implementing right now:
class Child < ActiveRecord::Base
belongs_to :parent
def self.update_info
self.with_lock
if true
# succesful
true
else
# fail
false
end
end
end
end
class Parent < ActiveRecord::Base
has_many :children
before_save :update_children
private
def update_children
self.with_lock do
self.children.each do |child|
if !child.update_info
raise SomeError.new
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment