Created
March 12, 2015 07:07
-
-
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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