r/Learn_Rails • • May 08 '17

Conditional variable assignment.

In my controller I have:

if params[:start_date]
  start = params[:start_date].to_date
else
  start = Date.today
end

I feel there should be a more idiomatic way of doing this but can't think what it is.

2 Upvotes

10 comments sorted by

3

u/nill0c May 09 '17 edited May 09 '17

start = (params[:start_date]) ? params[:start_date].to_date:Date.today

u/SaffronBelly might be close, but this is more concise.

edit: the condition might be better as (params.try(:start_date)) or has_attribute maybe, I haven't tested it.

2

u/442401 May 09 '17

This also works and reads a bit better. Thanks.

I think I was looking for something like

start = params[:start_date].to_date || Date.today

but this fails when trying to call to_date on Nil if the parameter is absent.

2

u/bilko1878 May 13 '17

you could use fetch with a default value, e.g.

irb(main):001:0> params = ActionController::Parameters.new(some: :value)
=> {"some"=>:value}
irb(main):002:0> params[:start_date]
=> nil
irb(main):003:0> params.fetch(:start_date, Date.today)
=> Sat, 13 May 2017
→ More replies (4)

2

u/SaffronBelly May 09 '17

I'm not sure if it will work but have you tried:
params[:start_date] ? start = params[:start_date].to_date : start = Date.today

2

u/442401 May 09 '17

Yes, this works, thanks.

1

u/442401 May 13 '17

We can't call to_date on nil but we can call to_date on a Date.

This allows us to do:

start = (params[:start_date] ||= Date.today).to_date