I have an Event model that, when saved, updates some attributes on the parent User.
class User
has_many :events
end
class Event
belongs_to :user
before_save :update_user_attributes
validates :user, presence: true
def update_user_attributes
self.user.update_attributes(hash_of_params)
end
end
This works fine most of the time — a User must exist and be logged in before they can interact with an Event.
But my test suite is causing problems, specifically the event factory.
FactoryGirl.define do
factory :event do
user
end
end
It seems that, due to the order FactoryGirl builds the Event, the User is not available at the time the Event is created, causing update_user_attributes
to fail.
This means that
create(:event)
# ActiveRecord::RecordNotSaved:
# Failed to save the record
but
build(:event).save
passes without errors
There are a number of ways I could prevent the error being raised, e.g., check that user.persisted?
in the update_user_attributes
method, or run the callaback after_save
. But my question specifically relates to FactoryGirl.
Given the above factoy, is there a way to force the associated User to be created before creating the Event?