How to access the curent factories attributes

24 views
Skip to first unread message

Jason Waldrip

unread,
May 22, 2012, 11:50:09 PM5/22/12
to factor...@googlegroups.com
Lets say I have the following factory:

FactoryGirl.define do
    
    factory :user do
        first_name: { Faker::Name.first_name }
        last_name: { Faker::Name.last_name }
        user_name: { "#{first_name.first}_#{last_name}" }
    end

end

Quite obviously this doesn't work, but how can I make it work to access the first_name and last_name attributes already generated above?
      


Josh Clayton

unread,
May 23, 2012, 9:53:59 AM5/23/12
to factory_girl
This should work just fine - the only issue I see is that you're
calling #first on first_name, where instead you'll probably want to do
first_name[0] or first_name.split(//).first.

Martin Streicher

unread,
May 23, 2012, 9:54:29 AM5/23/12
to factor...@googlegroups.com

You have it right. Your syntax is wrong, but the approach is proper. Here is the example from the docs.

factory :user do
  first_name "Joe"
  last_name  "Blow"
  email { "#{first_name}.#{last_name}@example.com".downcase }
end


--
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

Reply all
Reply to author
Forward
0 new messages