Get value of attribute from other attributes

Skip to first unread message

Carlos Chavez

Apr 16, 2013, 7:16:55 PM4/16/13
Hi All.

I'm trying create a factory that is able to get the value of the last id of the table and then use this for form the value of other field.

for example suppose the next value id if 1000, what I want is for example the value of the attributte name can be "name 1000"

is that possible?

I tried to the following config:

FactoryGirl.define do
  factory :lu_age do |la|

but the record is storing :

"AGE_DESCRIPTION #<FactoryGirl::Declaration::Implicit:0x0000000664b1c0>"

in the AGE_DESCRIPTION field

but what I want should be:  id=37, AGE_DESCRIPTION="AGE_DESCRIPTION 37"

Carlos Chávez.

Rennan Maciel

Apr 16, 2013, 10:54:23 PM4/16/13
you can use after :build to pass a block of code

factory :lu_age do
  # set normal fields here if any
  after :build do |la|
    # code you wish to have (example: la.description = "description")

still, Im not quite sure what you're trying to do

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
To unsubscribe from this group, send email to
For more options, visit this group at
You received this message because you are subscribed to the Google Groups "factory_girl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
For more options, visit

Rennan Maciel de Oliveira
Desenvolvedor Web na Superintendência de Tecnologia da Informação (STI)
Universidade Federal Fluminense (UFF)
Escritor wannabe nas horas vagas
+55 (21) 9248-8485

Carlos Chavez

Apr 16, 2013, 11:32:56 PM4/16/13
Hi Rennan.

I know I can use something like: sequence(:email) {|n| "person#{n}" } to fillup the field, but the problem with this is that n start in 1 or in a value that I can indicate, but I need to see that n start on the latest value that its on the database.

Suppose the primary key is age_id, what I need is to retrieve the latest value + 1 and use that value to fillup the other fields.

by the way, the code you provide throw the following:

[cchavez@dev01 pcd-bookeditor]$ RAILS_ENV=development rake test_data_strap:generate:master_series
rake aborted!
Both value and block given
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/factory_girl-3.2.0/lib/factory_girl/definition_proxy.rb:36:in `add_attribute'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/factory_girl-3.2.0/lib/factory_girl/definition_proxy.rb:91:in `method_missing'
/home/cchavez/scholastic/bitbucket/pcd-bookeditor/lib/test_data_strap/factories/lu_age.rb:4:in `block (2 levels) in <top (required)>'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/factory_girl-3.2.0/lib/factory_girl/syntax/default.rb:22:in `instance_eval'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/factory_girl-3.2.0/lib/factory_girl/syntax/default.rb:22:in `factory'
/home/cchavez/scholastic/bitbucket/pcd-bookeditor/lib/test_data_strap/factories/lu_age.rb:2:in `block in <top (required)>'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/factory_girl-3.2.0/lib/factory_girl/syntax/default.rb:16:in `instance_eval'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/factory_girl-3.2.0/lib/factory_girl/syntax/default.rb:16:in `run'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/factory_girl-3.2.0/lib/factory_girl/syntax/default.rb:7:in `define'
/home/cchavez/scholastic/bitbucket/pcd-bookeditor/lib/test_data_strap/factories/lu_age.rb:1:in `<top (required)>'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `block in load'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/factory_girl-3.2.0/lib/factory_girl/find_definitions.rb:20:in `block (2 levels) in find_definitions'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/factory_girl-3.2.0/lib/factory_girl/find_definitions.rb:19:in `each'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/factory_girl-3.2.0/lib/factory_girl/find_definitions.rb:19:in `block in find_definitions'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/factory_girl-3.2.0/lib/factory_girl/find_definitions.rb:15:in `each'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/factory_girl-3.2.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/gems/factory_girl-3.2.0/lib/factory_girl/reload.rb:8:in `reload'
/home/cchavez/scholastic/bitbucket/pcd-bookeditor/lib/test_data_strap/test_data_strap.rb:8:in `generate'
/home/cchavez/scholastic/bitbucket/pcd-bookeditor/lib/tasks/test_data_strap.rake:6:in `block (3 levels) in <top (required)>'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/bin/ruby_noexec_wrapper:14:in `eval'
/home/cchavez/.rvm/gems/ruby-1.9.3-p385@pcd-bookeditor/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => test_data_strap:generate:master_series
(See full trace by running task with --trace)
Carlos Chávez.

Rennan Maciel

Apr 17, 2013, 9:52:29 AM4/17/13

after_build do |person|
  next_id = Person.order("id desc") + 1 = "Person #{next_id}" = next_id + 1

correcting my example,
but not sure you'd want to set an ID on after_build though, as if you call 2 Factory.builds, you'll have 2 guys with name "Person 3", for example.

Carlos Chavez

Apr 17, 2013, 1:14:31 PM4/17/13
Hi Rennan.

I think that's fine, I will not set the ID, but only the other fields.

Oscar Rieken

Apr 17, 2013, 9:53:41 AM4/17/13
why not have a method that goes and gets the value from the db then pass that to the sequence

def  value_from_the_db
 get the last item from the db
factory :user do
  sequence(:email, value_from_the_db) {|n| "person#{n}" }
Reply all
Reply to author
0 new messages