nested after_create is passed a new record?

109 views
Skip to first unread message

fearless_fool

unread,
Aug 14, 2012, 9:28:54 PM8/14/12
to factor...@googlegroups.com
I've already posted to StackOverflow 
but I'm impatient and this is driving me nuts.

I can do this in the console:

>>> a = FactoryGirl.create(:client)
=> #<Client id: 4, name: "name5", email: "cli...@example.com", 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

but when I try to create an admin from FactoryGirl, I get:

>>> 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'

my factory looks like this:

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
I don't think I've changed any of my models, but I did update gems recently.  See the SO post for more details, but I thought I'd ask the experts to get a faster response.  Any idea what's going on?

- ff

fearless_fool

unread,
Aug 14, 2012, 9:47:52 PM8/14/12
to factor...@googlegroups.com
Solved.  I think FG changed the permissible syntax recently.  I used to have:

    factory :admin do
      after_create {|admin| 
        admin.assign_admin
      }
    end
    
and it *used* to work but not in the most recent version.  Changing it to 

    factory :admin do
      after(:create) {|admin| 
        admin.assign_admin
      }
    end
    
made everything work again.
Reply all
Reply to author
Forward
0 new messages