fearless_fool
fearless_fool

Reputation: 35189

Getting "You cannot call create unless the parent is saved" error with FactoryGirl

This is driving me nuts. FactoryGirl has stopped working, but I can't see why or how -- perhaps a gem update did me in? First the problem, then the details:

>>> c = FactoryGirl.create(:client)
=> #<Client id: 3, name: "name3", email: "[email protected]", password_digest: "$2a$10$iSqct/0DIQbL.OcRrYOiPuiKijbAXggLxcMevS3TmVIV...", created_at: "2012-08-14 23:25:22", updated_at: "2012-08-14 23:25:22">
>>> a = FactoryGirl.create(:admin)
ActiveRecord::RecordNotSaved: You cannot call create unless the parent is saved
        from /.../usr/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/associations/collection_association.rb:425:in `create_record'

Debugging print statements tell me that the :admin is a new_record?, so naturally I can't form an association with it. Here's the Client Factory. The idea is that an admin is just a client which has been assigned Admin privileges:

FactoryGirl.define do
  factory :client do
    sequence(:name) {|n| "name#{n}"}
    sequence(:email) {|n| "client#{n}@example.com" }
    password "password"

    factory :admin do
      after_create {|admin| 
        admin.assign_admin
      }
    end

  end

end

When I replicate in the console what FactoryGirl is doing (or should be doing), everything works okay:

>>> a = FactoryGirl.create(:client)
=> #<Client id: 4, name: "name5", email: "[email protected]", password_digest: "$2a$10$vFsW6VfmNMKWBifPY3vcHe6Q2.vCCLEq3RqPYRxdMo0m...", created_at: "2012-08-14 23:37:11", updated_at: "2012-08-14 23:37:11">
>>> a.assign_admin
=> #<ClientRole id: 2, client_id: 4, role: 1, created_at: "2012-08-14 23:37:28", updated_at: "2012-08-14 23:37:28">
>>> a.admin?
=> true

Here's the Client model:

class Client < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password
  validates_presence_of :name, :email, :password, :on => :create
  validates :name, :email, :uniqueness => true
  has_many :sites, :dependent => :destroy
  has_many :client_roles, :dependent => :destroy
  # roles

  def has_role?(role)
    client_roles.where(:role => role).exists?
  end

  def assign_role(role)
    client_roles.create(:role => role) unless has_role?(role)
  end

  def revoke_role(role)
    client_roles.where(:role => role).destroy_all
  end

  def assign_admin
    assign_role(ClientRole::ADMIN)
  end

  def admin?
    has_role?(ClientRole::ADMIN)
  end

end

And for completeness, the ClientRole model:

class ClientRole < ActiveRecord::Base
  belongs_to :client

  # values for #role
  ADMIN = 1

end

And finally, version and dependency info from Gemfile.lock:

factory_girl (4.0.0)
  activesupport (>= 3.0.0)
factory_girl_rails (4.0.0)
  factory_girl (~> 4.0.0)
  railties (>= 3.0.0)

Upvotes: 2

Views: 4110

Answers (1)

fearless_fool
fearless_fool

Reputation: 35189

Solved. Something changed in the most recent version of FG. The following factory method used to work:

factory :admin do
  after_create {|admin| 
    admin.assign_admin
  }
end

but the most recent documentation says the syntax is now:

factory :admin do
  after(:create) {|admin| 
    admin.assign_admin
  }
end

Changing it makes everything work. whew.

Upvotes: 9

Related Questions