Skip to content

Instantly share code, notes, and snippets.

@gaspard
Created February 10, 2009 15:23
Show Gist options
  • Save gaspard/61423 to your computer and use it in GitHub Desktop.
Save gaspard/61423 to your computer and use it in GitHub Desktop.
# Returns whether or not the parent, <tt>self</tt>, and any loaded autosave associations are valid.
def valid_with_autosave_associations?
record_valid = valid_without_autosave_associations?
self.class.reflect_on_all_autosave_associations.all? do |reflection|
if (association = association_instance_get(reflection.name)) && association.loaded?
if association.is_a?(Array)
association.proxy_target.all? { |child| autosave_association_valid?(reflection, child) }
else
autosave_association_valid?(reflection, association)
end
else
true # association not loaded yet, so it should be valid
end
end && record_valid
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment