I had a similar problem, checking if the has_many association had at least one related model:
class Classroom < ActiveRecord::Base # Relationships belongs_to :course has_many :weekdays # Validations validate :has_weekdays def has_weekdays errors.add(:base, 'it is invalid without at least one weekday') if self.weekdays.blank? end end
So, to make the factory valid, here is my code
FactoryGirl.define do factory :classroom do course after (:build) do |classroom| classroom.weekdays << FactoryGirl.create(:weekdays, classroom: classroom) end end end
source share