I'm trying to create a parent object with the set of nested child objects associated to it, however I'm not sure how to access the parent object (other children) in validation methods of the child object before either object is persisted. Here is my scenario in a nutshell:
class Parent < ActiveRecord::Base
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :parent
validate :siblings_validation
def siblings_validation
siblings = parent.children #parent is Nil here on create (but not on update)
# compare stuff...
end
end
The association is created with the standard set of nested forms, something like that:
- form_for @parent do |f|
= f.text_field :name
- f.fields_for :children do |c|
= render :partial => "child_fields", :locals => {:f => c}
Is there a way to access a parent from siblings_validation method of the child object?
Thanks for any responses.