thoughtbot / factory_bot

A library for setting up Ruby objects as test data.

Home Page:https://thoughtbot.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dependent Attributes when field name is capitalized?

toomanyjoes opened this issue · comments

I'm working with an unfortunately designed legacy mysql database that uses capitals in field names. The problem is that rails thinks these are constants so I can't use them in my Factory Bot dependent attributes:

FactoryBot.define do
  factory :'legacy/cllnadjhistory', class: Legacy::Cllnadjhistory do
    CLI_NO { Faker::Number.number(digits: 5) }
    FOLIO { Faker::Number.number(digits: 5) }
    NOTE { "Chrgs" }
    AMOUNT { Faker::Number.decimal(l_digits: 2) }
    LADESCR { "Charge" }
    OperationsDate { Faker::Date.between(from: '2014-01-01', to: DateTime.current.strftime("%Y-%m-%d")) }
    DateRecordAdded { OperationsDate }
  end
end

I get 'uninitialized constant OperationsDate' on the DateRecordAdded { OperationsDate } line.

Is there a way to explicitly tell factorybot this is a field name in the database and not a constant?

Thanks!

Yeah, Ruby is always going to parse that as a constant. You might be able to get what you want by turning that into a method call instead (if I recall correctly self inside the block is the FactoryBot::Evaluator object that has methods for each attribute)

    DateRecordAdded { self.OperationsDate }

That did the trick! Thank you @composerinteralia!