Hey again. We have one small issue that we're having trouble with. We have a has_many through relationship that we validate presence of. Even when using the "after_build" callback, the validation seems to fail. Here's the relevant code:
has_many :user_roles
has_many :roles, through: :user_roles
validates :first_name, :last_name, :user_roles, presence: true
end
class UserRole < ActiveRecord::Base
belongs_to :user
belongs_to :role
end
class Role < ActiveRecord::Base
has_many :user_roles
has_many :users, through: :user_roles
end
Fabricator
Fabricator(:user) do
first_name "Person"
last_name "McFace"
email {...}
paypal_email {...}
password "woah-plain-text"
rate { (30..50).to_a.sample }
after_build do |user|
Fabricate(:user_role, :user => user, :role => Fabricate(:consultant_role))
end
end
Error
ActiveRecord::RecordInvalid: Validation failed: User roles can't be blank
The above sort of makes sense.. because when it tries to insert the 'user_role' into the database, the 'user_id' is actually nil at that point (because the user wasn't created yet.)
I've generated the same data in ActiveRecord by using 'user_role_attributes'. Like so:
That works, but I don't know how to accomplish something similar in Fabrication.
Thanks for any help/insight you can provide!