add_attribute': Both value and block given

1,918 views
Skip to first unread message

kadoudal

unread,
May 9, 2012, 1:25:51 PM5/9/12
to factory_girl
I am trying to add (my first) after(;create in a factory, and I get
this error ...

gems/factory_girl-3.2.0/lib/factory_girl/definition_proxy.rb:36:in
`add_attribute': Both value and block given
(FactoryGirl::AttributeDefinitionError)

I have an Admin model
class Admin < ActiveRecord::Base
include RoleModel
.....
end

I am creating an Admin instance with many attributes then adding roles
to it
I usually do
superadmin = Admin.create(...attributes...)
superadmin.roles << [:superadmin] ( array )

to create w FactoryGirl, I wrote :
FactoryGirl.create(:admin, ..(attributes) ... , roles:
[:superadmin] )


the factory is the following :

FactoryGirl.define do
factory :admin do
ignore do
roles
end
.....
after(:create) { |admin| admin.roles << roles }
end

seems to be wrong .... any feedback on it ? thanks

kadoudal

unread,
May 10, 2012, 2:42:02 AM5/10/12
to factory_girl
I guess I found a trick ( or may it's the way it should be done ..)

not using the after(:create) callback but rather adding :
factory :superadmin, :class => Admin do |a|
a.roles ['superadmin']
end

and creating the instance with like this :
FactoryGirl.create(:superadmin, email: .... )

Is that the way it should be done ?

Joe Ferris

unread,
May 10, 2012, 9:49:20 AM5/10/12
to factor...@googlegroups.com
Hey,

The new callback syntax hasn't been released yet. You'll want to write your callbacks like this:

    after_create { |instance| … }

The documentation on Github can be confusing, since it shows everything in master, not all of which is released.

-Joe
--
Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:

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
For more options, visit this group at

kadoudal

unread,
May 10, 2012, 10:15:55 AM5/10/12
to factory_girl
Hi

thanks a lot for this info... yeah, the documentation is not as
complete as a beginner would like to read ... ;-))
but that's not a big deal as I found a 'trick' ... I'll modify it
after the new callback syntax will be released ..

I have another issue in understanding how associations can be
defined ...

I am trying to create a Region inside an Area ' Area has_many
Region

I defined an Area Factory , creating instances with
FactoryGirl.create(:area, name: area_name) running OK

I defined a Region factory
FactoryGirl.define do
factory :region do
area
name
..
end

but trying to create a Region instance with :
area = Area.first => #<Area id: 18, name: "My New Area"..>
insee_code = "53"
new_region = FactoryGirl.create(:region, insee_code: region, area:
area)
I always get an error : TypeError: Cannot visit Area

If I use FactoryGirl.build(:region, insee_code: region, area: area), I
can see the region instance built ..
#<Region id: nil, area_id: 18, name: "Brittany",..>
so what happen with the save ?

I tried to modify the association ,association :area, factory: :area,
strategy: :build...
but same issue ..
where is it documented ?

thanks for your feedback





On May 10, 3:49 pm, Joe Ferris <jfer...@thoughtbot.com> wrote:
> Hey,
>
> The new callback syntax hasn't been released yet. You'll want to write your callbacks like this:
>
>     after_create { |instance| … }
>
> The documentation on Github can be confusing, since it shows everything in master, not all of which is released.
>
> -Joe
>
>
>
>
>
>
>
> On Thursday, May 10, 2012 at 2:42 AM, kadoudal wrote:
> > I guess I found a trick ( or may it's the way it should be done ..)
>
> > not using the after(:create) callback but rather adding :
> > factory :superadmin, :class => Admin do |a|
> > a.roles ['superadmin']
> > end
>
> > and creating the instance with like this :
> > FactoryGirl.create(:superadmin, email: .... )
>
> > Is that the way it should be done ?
>
> > To post to this group, send email to factor...@googlegroups.com (mailto:factor...@googlegroups.com)
> > To unsubscribe from this group, send email to
> > factory_girl...@googlegroups.com (mailto:factory_girl...@googlegroups.com)

kadoudal

unread,
May 10, 2012, 11:10:48 AM5/10/12
to factory_girl
[SOLVED] not related to FactoryGirl association, but rather to a
uniqueness validation with a scope ..
I was using the association name ... :scope => :area WRONg , should
be :scope => :area_id

tea time now !

On May 10, 3:49 pm, Joe Ferris <jfer...@thoughtbot.com> wrote:
> Hey,
>
> The new callback syntax hasn't been released yet. You'll want to write your callbacks like this:
>
>     after_create { |instance| … }
>
> The documentation on Github can be confusing, since it shows everything in master, not all of which is released.
>
> -Joe
>
>
>
>
>
>
>
> On Thursday, May 10, 2012 at 2:42 AM, kadoudal wrote:
> > I guess I found a trick ( or may it's the way it should be done ..)
>
> > not using the after(:create) callback but rather adding :
> > factory :superadmin, :class => Admin do |a|
> > a.roles ['superadmin']
> > end
>
> > and creating the instance with like this :
> > FactoryGirl.create(:superadmin, email: .... )
>
> > Is that the way it should be done ?
>
> > To post to this group, send email to factor...@googlegroups.com (mailto:factor...@googlegroups.com)
> > To unsubscribe from this group, send email to
> > factory_girl...@googlegroups.com (mailto:factory_girl...@googlegroups.com)
Reply all
Reply to author
Forward
0 new messages