# Pull full date from group\_by

**URL:** <https://www.ruby-forum.com/t/pull-full-date-from-group-by/144405>\
**Category:** Rails\
**Created:** [July 29, 2008, 12:39pm UTC](https://www.ruby-forum.com/t/pull-full-date-from-group-by/144405 "2008-07-29T12:39:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jackson](https://www.ruby-forum.com/u/jackson)\
**Post date:** [July 29, 2008, 12:39pm UTC](https://www.ruby-forum.com/t/pull-full-date-from-group-by/144405/1 "2008-07-29T12:39:55Z")

</div>

Hello there,

I started with to\_set.classify and have the find function grouping by  
day. I then did a group\_by function to the same effect both in the  
model and the view.

They both work wonderfully except I need to pull the full date from it  
not just the number of the day.

controller

@groups = Lineup.find(:all, :conditions =\> search\_conditions, :limit  
=\> 400, :include =\> [{:event =\> :venue}, {:artist  
=\> :genre}, :artist]).to\_set.classify {|lineup|  
lineup.event.event\_time.day}.sort

In the view I have two loops set to run through the day then the  
lineups.

strftime will not work since it is a fix num but I need the month of  
the day and the name of the day.

Any ideas? Thanks for your help!

Best,

Jackson

---

<div class="post-metadata">

**Author:** ![jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jackson](https://www.ruby-forum.com/u/jackson)\
**Post date:** [July 30, 2008, 12:18pm UTC](https://www.ruby-forum.com/t/pull-full-date-from-group-by/144405/2 "2008-07-30T12:18:47Z")

</div>

I do have the same question, did you find any answer ?

thanks

---

<div class="post-metadata">

**Author:** ![jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jackson](https://www.ruby-forum.com/u/jackson)\
**Post date:** [July 30, 2008, 9:08pm UTC](https://www.ruby-forum.com/t/pull-full-date-from-group-by/144405/3 "2008-07-30T21:08:34Z")

</div>

def day  
self.event.event\_time.strftime("%y%j%d%B%A")  
end

This will sort by year to account for January then the day number.

---

<div class="post-metadata">

**Author:** ![jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jackson](https://www.ruby-forum.com/u/jackson)\
**Post date:** [July 30, 2008, 9:08pm UTC](https://www.ruby-forum.com/t/pull-full-date-from-group-by/144405/4 "2008-07-30T21:08:34Z")

</div>

I have it pulling using a method that I created in the model.

def day  
self.event.event\_time.strftime("%B%d%A")  
end

That spits out the string that I require then I parse it with reg ex  
and match.

In the view I have:

\<% @groups.group\_by(&:day).each do |day, lineups| %\>

Now the problem is sorting. If I sort in the view, I’m sorting by  
strings.

In the controller:

@groups = Lineup.find(:all, :conditions =\> search\_conditions, :limit  
=\> 400, :include =\> [{:event =\> :venue}, {:artist  
=\> :genre}, :artist]).sort\_by{|e| e.event.event\_time}

I can sort by time but it only sorts the individual lineups, the group  
by day seems to be unaffected.

Dates are fun…

Can anyone provide some direction?

Thanks,

Jackson

---

<div class="post-metadata">

**Author:** ![jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jackson](https://www.ruby-forum.com/u/jackson)\
**Post date:** [July 31, 2008, 5:06am UTC](https://www.ruby-forum.com/t/pull-full-date-from-group-by/144405/5 "2008-07-31T05:06:27Z")

</div>

Yeah,

Went well!

controller::

@groups = Lineup.find(:all, :conditions =\> search\_conditions, :limit  
=\> 400, :include =\> [{:event =\> :venue}, {:artist  
=\> :genre}, :artist]).sort\_by{|e| e.event.event\_time}

model::

def day  
self.event.event\_time.strftime(“%y%j%d%B%A”)  
end

view::

\<% @groups.group\_by(&:day).sort.each do |day, lineups| %\>

\<%= day.match(‘((?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|  
May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:t|ember)?|Oct(?:ober)?|  
Nov(?:ember)?|Dec(?:ember)?))’).to\_s.upcase %\>

\<%= this.event.event\_time.strftime(“%I” + “:” + “%M%p”) %\>

\<% end %\>

The second repeat is hobo dryml but it should make sense.

The controller call sorts every return. The model’s definition was  
key. I had to find all by day using the strftime function but needed  
more info than just the day number. I put month, day, day full name.  
That was tough to sort because you trade in the datetime class for a  
string, I added year then day of the year so the records would come  
back in order. The view I pulled the substrings using regex matches.  
([http://txt2re.com/](http://txt2re.com/))

I hope this helps. Let me know if you have any questions, I’ll see  
what I can do.

Best,

Jackson
