undefined method for #<FactoryGirl::Declaration::Implicit

891 views
Skip to first unread message

kadoudal

unread,
Nov 12, 2012, 9:23:22 AM11/12/12
to factor...@googlegroups.com
I am trying to reference an implicit parameter after creating a factory instance

 account is an implicit parameter :  FactoryGirl.create(:site, account: @account)  or FactoryGirl.create(:site)
but it seems I cannot access it in the after_create block .. any clue ?


FactoryGirl.define do
  factory :site  do |s|
    ignore do
     account { FactoryGirl.create(:account) }
    end
......
    after(:create) do |site| 
      subdomain = FactoryGirl.build(:subdomain, ...)
      s.account.subdomains << subdomain
    end
  end
end

kadoudal

unread,
Nov 12, 2012, 9:47:53 AM11/12/12
to factor...@googlegroups.com
I tried rewriting it this way, wo any success => Exception: undefined local variable or method `account_id'

FactoryGirl.define do
  factory :site  do |cs|
    ignore do
     account_id { FactoryGirl.create(:account).id }
    end
......
    after(:create) do |site| 
      subdomain = FactoryGirl.build(:subdomain, ...)
 account = Account.find(account_id)
      account.subdomains << subdomain
    end
  end
end

kadoudal

unread,
Nov 12, 2012, 10:24:03 AM11/12/12
to factor...@googlegroups.com
[SOLVED]    account is not an attribute of Site..  and I don't want to modify the Site model to add it  ( in a gem)
so I resolved to take it out of the Factory (keeping it simple) and modify my test

    @site = FactoryGirl.create(:site)
    @subdomain = FactoryGirl.create(:subdomain, site: @site )
    @account.subdomains << @subdomain



Le lundi 12 novembre 2012 15:23:22 UTC+1, kadoudal a écrit :

Keenan Brock

unread,
Nov 12, 2012, 11:34:20 AM11/12/12
to factor...@googlegroups.com
Hi,

Looks like you're going in the right direction

Something to keep in mind for the future:

after(:create) takes 2 parameters, the object being build and the parameter context, which is often called the evaluator.


FactoryGirl.define do
  factory :account do
  end
  factory :subdomain do
  end

  factory :site  do
    association :subdomain

    ignore do
     account { FactoryGirl.create(:account) } #maybe this should be a build
    end

    after(:create) do |site, evaluator|
      evaluator.account.subdomains << site.subdomain
    end
  end
end

Hope that helps
--
Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:
http://thoughtbot.com/services/training
 
You received this message because you are subscribed to the "factory_girl" mailing list.
To post to this group, send email to factor...@googlegroups.com
To unsubscribe from this group, send email to
factory_girl...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/factory_girl?hl=en

kadoudal

unread,
Nov 17, 2012, 8:59:42 AM11/17/12
to factor...@googlegroups.com
Thanks helpful indeed
Reply all
Reply to author
Forward
0 new messages