I’m using accepts_nested_attributes_for
with a reject_if
and I’m trying to access a relationship from the parent that’s holding the nested attributes from inside the reject_if
function. When I do this however, it seems that a belongs_to
relationship returns nil. I’ve got a quick example here:
class Car
belongs_to :manufacturer
accepts_nested_attributes_for :wheels, reject_if :reject_if_blank_and_ford
def reject_if_blank_and_ford(attributes)
return false if attributes[:wheel_size].blank? and self.manufacturer.make == "Ford"
end
end
The issue is that when doing:
manufacturer = Manufacturer.find_by_make("Ford")
manufacturer.cars.create(params[:car])
in the reject_if
, self.manufacturer
is nil. If I try it out with a before_validation:
before_validation do
self.manufacturer.make
end
This works. It leads me to believe that the relationship to manufacturer is instantiated/setup AFTER the reject_if
- is there any way to access this relationship in the reject_if
block?