Ruby on Rails Monday, August 29, 2011

On Aug 29, 9:45 am, comopasta Gr <li...@ruby-forum.com> wrote:
> Frederick Cheung wrote in post #1018934:

> With a "normal" query it goes ok. Bit I had an issue with something
> like:
>
> @total_visits_by_day = Visit.where("tag_id = ?",
> identifier).order("DATE(created_at)").group("DATE(created_at)").count
>
> I couldn't find a way to feed in_time_zone to the created_at in the
> query.
>

That's because when mysql computes DATE(created_at) it knows nothing
about Rails' Time.zone.
Mysql has its own timezone conversion functions, so you can do stuff
like DATE(CONVERT_TZ(created_at, 'UTC', 'Europe/London') ) which
converts the time to the requested timezone before manipulating it

Fred

> I solved that by first making a "normal" request and then grouping that:
> visits = Visit.where(:tag_id => identifier).all
> @total_visits_by_day = visits.group_by{|day|
> day.created_at.strftime("%Y-%m-%d")}.map{|k,v| [k, v.length]}
>
> So now I'm getting the dates converted to the user's time zone.
>
> --
> Posted viahttp://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment