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.
Answers to your questions
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.
I’m not sure if it will work but have you tried:
params[:start_date] ? start = params[:start_date].to_date : start = Date.today
`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.
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