I have a table that has a date column price_to_start_on.
I use a controller that gets data using the model, and renders to xml,
the date comes out as yyyy_mm_dd format. I want the format to be
yyyymmdd.
How do I do this, is it in the model? or the controller?
Ideally I would like it in the model, as it only has to be coded once.
Thanks
Andrew
Andrew C. wrote:
I have a table that has a date column price_to_start_on.
I use a controller that gets data using the model, and renders to xml,
the date comes out as yyyy_mm_dd format. I want the format to be
yyyymmdd.
How do I do this, is it in the model? or the controller?
Use the Time#strftime method to convert a date/time to a string using a
custom format. Something like
rec.price_to_start_on.strftime(’%Y%m%d’)
should work for you. BTW, #strftime will cause an error if you call it
on nil, so you might be better off checking for nil unless this column
has a not-null constraint:
rec.price_to_start_on && rec.price_to_start_on.strftime(’%Y%m%d’)
Ideally I would like it in the model, as it only has to be coded once.
I’d put it in the view (if you have one), otherwise in the controller –
since the format is related to the output, not to the underlying data
(which is usually what the model is all about).
You can’t guarantee that you’re not going to want it in a different
format later on for some other purpose. Putting it into the
controller/view won’t lock you into a format for all future uses.
Thanks
Andrew
Uhm, correct me if i’m wrong, cos i’m all new to RoR + Ruby itself, but
i think you could add a method to your usermodel that converts the
date, like this:
class BlaBla < ActiveRecord::Base
…
…
def price_to_start_on_convdate
self.price_to_start_on.strftime("%Y%m%d")
#or like this? gotta learn proper Ruby, damn 
return self.price_to_start_on.strftime("%Y%m%d")
end
end
the in a view you could just use it like this:
<%= someObject.price_to_start_on_convdate %>
Hope that helps
I did add a method to the model as suggested. But I am not using views
so have added the formatted date to the array of results. The data is
then rendered as XML and is then available to my Flex application.
Thanks for the help.
Andrew
Thorsten L wrote:
Uhm, correct me if i’m wrong, cos i’m all new to RoR + Ruby itself, but
i think you could add a method to your usermodel that converts the
date, like this:
class BlaBla < ActiveRecord::Base
…
…
def price_to_start_on_convdate
self.price_to_start_on.strftime("%Y%m%d")
#or like this? gotta learn proper Ruby, damn 
return self.price_to_start_on.strftime("%Y%m%d")
end
end
the in a view you could just use it like this:
<%= someObject.price_to_start_on_convdate %>
Hope that helps
Is it correct to put that kind of thing in the model? I did that before
with a date format - like Object#displaydate, and I was told that it was
bad form - I should put it in a helper instead. Any thoughts on this?
i don’t really think there is a “wrong” or “right” for questions like
this, it mainly is a matter of taste.
both solutions would be DRY in my opinion, the model-way adds a method
to your object while a helper would be something like this:
<%= display_date(@yourobject.date) %>