# Factory\_girl association with specific values

**URL:** <https://www.ruby-forum.com/t/factory-girl-association-with-specific-values/195577>\
**Category:** Rails\
**Created:** [September 25, 2010, 9:05pm UTC](https://www.ruby-forum.com/t/factory-girl-association-with-specific-values/195577 "2010-09-25T21:05:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [September 25, 2010, 9:05pm UTC](https://www.ruby-forum.com/t/factory-girl-association-with-specific-values/195577/1 "2010-09-25T21:05:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [September 26, 2010, 10:07am UTC](https://www.ruby-forum.com/t/factory-girl-association-with-specific-values/195577/2 "2010-09-26T10:07:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [September 26, 2010, 8:31pm UTC](https://www.ruby-forum.com/t/factory-girl-association-with-specific-values/195577/3 "2010-09-26T20:31:40Z")

</div>

2010/9/26, Peter De Berdt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 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 removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid.  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
[www.webvolkan.blogspot.com](http://www.webvolkan.blogspot.com)  
volkan Ã¶zdemirintelefon numarasÄ±  
05319953770

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [September 27, 2010, 5:27am UTC](https://www.ruby-forum.com/t/factory-girl-association-with-specific-values/195577/4 "2010-09-27T05:27:50Z")

</div>

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

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