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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 Sep 2010, at 21:04, David Kahn wrote:> 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'' > endSince 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2010/9/26, Peter De Berdt <peter.de.berdt-LPO8gxj9N8aZIoH1IeqzKA@public.gmane.org>:> > On 25 Sep 2010, at 21:04, David Kahn wrote: > >> 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 > > 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 > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- www.webvolkan.blogspot.com volkan özdemirintelefon numarası 05319953770 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks Peter, that makes a lot of sense and worked perfectly! On Sun, Sep 26, 2010 at 3:05 AM, Peter De Berdt <peter.de.berdt-LPO8gxj9N8aZIoH1IeqzKA@public.gmane.org>wrote:> > On 25 Sep 2010, at 21:04, David Kahn wrote: > > 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 > > > 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 > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Possibly Parallel Threads
- preload test data from factory_girl
- Validation Failed: Userkey has already been taken, Email has already been taken
- RSpec / Cucumber painfully slow Rails 3 OSX
- How do I deal with ActiveRecord::RecordInvalid: Validation failed:
- uninitialized constant - Please Help Me...