Factory_girl association with specific values

Hope this should be simple: I have a User model and a Role model.
Factories
for each.

When I create a User using FG, I want to assign a specific role. Cant
seem
to figure out how to do this, getting errors like: uninitialized
constant
SysadminRole for doing things this way:

Factory.define :user do |u|
u.practice_id { |a| a.association(:practice).id }
u.password ‘password1’
u.password_confirmation ‘password1’
u.role { |a| a.association(:sysadmin_role) }
end

Factory.define :sysadmin_role do |f|
f.name ‘sysadmin’
end

Thanks,

David

On 25 Sep 2010, at 21:04, David K. wrote:

u.password_confirmation ‘password1’
u.role { |a| a.association(:sysadmin_role) }
end

Factory.define :sysadmin_role do |f|
f.name ‘sysadmin’
end

Since your model is called role and not sysadmin_role, you need to let
FactoryGirl know. Otherwise it will use its convention of
constantizing the symbol after “define” to determine the model to call.

Factory.define :sysadmin_role, :class => “Role” do |f|

Also good to know is that if you want to use factories for roles that
have some fields in common and others that are specific, that you can
use this:

Factory.define :role do |f|
f.common_field “foobar”
f.name “guest”
end

Factory.define :sysadmin_role, :parent => :role do |f|
f.name “sysadmin”
end

Factory.define :moderator_role, :parent => :role do |f|
f.name “moderator”
end

Best regards

Peter De Berdt

2010/9/26, Peter De Berdt [email protected]:

Factory.define :user do |u|
Since your model is called role and not sysadmin_role, you need to let
f.common_field “foobar”

To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


www.webvolkan.blogspot.com
volkan özdemirintelefon numarası
05319953770

Thanks Peter, that makes a lot of sense and worked perfectly!

On Sun, Sep 26, 2010 at 3:05 AM, Peter De Berdt

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs