I have two models:
class Shift < ActiveRecord::Base
attr_accessible :ranges_attributes
has_many :ranges
accepts_nested_attributes_for :ranges, allow_destroy: true
end
class Range < ActiveRecord::Base
belongs_to :shift
validates :shift, presence: true
end
When, in my controller, I want to create a shift with ranges I'm getting:
Shift.create! params[:shift]
#ActiveRecord::RecordInvalid Exception: Validation failed: Shift ranges shift can't be blank
If I remove validates :shift, presence: true
from Range
model this works beautifully. I'm able to create a new shift with his children. ActiveRecord
does that for me.
The question is: why do I need to remove that validation to make this work?