# Will\_paginate with group\_by

**URL:** <https://www.ruby-forum.com/t/will-paginate-with-group-by/156337>\
**Category:** Rails\
**Created:** [December 24, 2008, 7:14pm UTC](https://www.ruby-forum.com/t/will-paginate-with-group-by/156337 "2008-12-24T19:14:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [December 24, 2008, 7:14pm UTC](https://www.ruby-forum.com/t/will-paginate-with-group-by/156337/1 "2008-12-24T19:14:26Z")

</div>

I am trying to use will\_paginate with group\_by but i keep getting  
“undefined method `total\_pages’ for”.

–Controller–

@records = Record.paginate( :conditions =\> [‘this\_id = ? and that\_id =  
?’,session[:this\_id],session[:that\_id]], :page =\> params[:page],:order  
=\> ‘created\_at DESC’, :per\_page =\> 2).group\_by { |c|  
c.created\_at.to\_date }

–view–

\<% @records.each do |d, records| %\>  
**\<%= d %\>**  
\<% for r in records %\>

\<%= link\_to r.title, :action =\> “action”, :id =\> r.id %\>

  
\<% end %\>  
\<% end %\>  
\<%= will\_paginate @records %\>

any help is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [December 25, 2008, 7:26am UTC](https://www.ruby-forum.com/t/will-paginate-with-group-by/156337/2 "2008-12-25T07:26:42Z")

</div>

Silly me!

Here is how I should have written it.

1. Remove the group\_by from controller
2. Place it in view

–Controller–

@records = Record.paginate( :conditions =\> [‘this\_id = ? and that\_id =  
?’,session[:this\_id],session[:that\_id]], :page =\> params[:page],:order  
=\> ‘created\_at DESC’, :per\_page =\> 2)

–view–

\<% @records.group\_by { |c|  
c.created\_at.to\_date }.each do |d, records| %\>  
**\<%= d %\>**  
\<% for r in records %\>

\<%= link\_to r.title, :action =\> “action”, :id =\> r.id %\>

  
\<% end %\>  
\<% end %\>  
\<%= will\_paginate @records %\>
