I have a Listing
model which has_many :categories, through: :categories_listings
and has_many :categories_listings
. I was testing it with a Factory Girl factory which looks like this:
factory :listing do |f|
f.sequence (:title) { |n| "Listing #{n}" }
message {Faker::Lorem.paragraph}
cards {[FactoryGirl.create(:card)]}
categories {[FactoryGirl.create(:category)]}
association :delivery_condition
association :unit_of_measure
quantity 1
unit_price 1
end
And everything was OK until I added the following validation to the model:
validate :has_categories?
def has_categories?
if self.categories_listings.blank?
errors.add :base, "You have to add at least one category"
end
end
Now whenever I run the factory I get:
ActiveRecord::RecordInvalid: You have to add at least one category
I also tried with Factory Girl callbacks like before :create
but the problem is that I can't add the association because I don't yet know the listing id in the callback. But I can't save the listing because validations are run before the association.
How can I get around this and make it work?