Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write factorygirl for table that have fields containing ruby keyword?

I have a table with one of the field named end. How do I write the factory for that table:

I try to use quote but it's not working:

FactoryGirl.define do
  factory :open_hour do
    location
    day { Date::DAYNAMES.sample.downcase }
    start { Time.now.strftime('%H:%M') }
    'end' { Time.now.strftime('%H:%M') }
  end
end
like image 422
Donny Kurnia Avatar asked Oct 20 '25 05:10

Donny Kurnia


2 Answers

From FactoryGirl docs:

factory :user do
  name 'Billy Idol'
end

and:

factory :user do
  add_attribute :name, 'Billy Idol'
end

are equivalent.

like image 54
Amadan Avatar answered Oct 21 '25 20:10

Amadan


Having had some awful issues in the past when trying to do this, I'd advise that you rename those fields to 'start_time' and 'end_time'. There may be other unanticipated issues caused by having fields/methods named 'end', and tracking them down can be extremely annoying. 'end_time' also has the virtue of more clearly explaining what it is.

like image 44
geoffharcourt Avatar answered Oct 21 '25 22:10

geoffharcourt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!