Topic: How to use will_paginate on this code?

I can't get to seem to wrap my head around this so if you could point me to the right direction, that would be really appreciated.

I have a method class in my category model

def with_categorized_listing
  directories.find(:all, :include => :categorizations, :joins => 'INNER JOIN companies c ON company_id = c.id', :order => 'featured DESC')
end

<% for listing in @category.with_categorized_listing %>
<p><b><%= listing.company.name %></b></p>
<p>
  <%=h listing.address1 %> <%=h listing.address2 -%>,<br />
  <%=h listing.city %>, <%=h listing.state %> <%=h listing.zipcode %>
</p>
<% end %>

Is it still possible to implement will_paginate to this?

=====================
Sam G.

Re: How to use will_paginate on this code?

You need to use the "paginate" method instead of the "find" method. Also, I recommend setting this in the controller instead of the view:

# controller
@listings = @category.with_categorized_listing

That way it's easier to handle the pagination.

Railscasts - Free Ruby on Rails Screencasts

Re: How to use will_paginate on this code?

Welcome back!

Thanks for the help. I'll refactor that one. ;p

=====================
Sam G.

Re: How to use will_paginate on this code?

# category model -- class
def with_categorized_listing(page)
  directories.paginate(:all, :include => :categorizations, :joins => 'INNER JOIN companies c ON company_id = c.id', :order => 'featured DESC',:per_page => 10,:page => params[:page]) 
end

# controller
def show
  @category = Category.find(params[:id])
  @listings = @category.with_categorized_listing(params[:page])
...
end

# view
<% for listing in @listings %>
<p><b><%= listing.company.name %></b></p>
<p>
  <%=h listing.address1 %> <%=h listing.address2 -%>,<br />
  <%=h listing.city %>, <%=h listing.state %> <%=h listing.zipcode %>
</p>
<% end %>

<%= will_paginate @listings %>


But I just get this error now:

NameError in CategoriesController#show

undefined local variable or method `params' for #<Category:0x2b0de3d26ab0>

RAILS_ROOT: config/..
Application Trace | Framework Trace | Full Trace

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1860:in `method_missing'
app/models/category.rb:54:in `with_categorized_listing'
app/controllers/categories_controller.rb:21:in `show'

=====================
Sam G.

Re: How to use will_paginate on this code?

Make sure to use the passed page variable instead of calling params[:page] in your Category model.

Railscasts - Free Ruby on Rails Screencasts

Re: How to use will_paginate on this code?

I don't understand what you trying to say Ryan. I'm a little confused now.

=====================
Sam G.

Re: How to use will_paginate on this code?

Use this code in your category model:

def with_categorized_listing(page)
  directories.paginate(:all, :include => :categorizations, :joins => 'INNER JOIN companies c ON company_id = c.id', :order => 'featured DESC',:per_page => 10,:page => page) 
end

The difference is how the page is passed.

Railscasts - Free Ruby on Rails Screencasts

Re: How to use will_paginate on this code?

Oh!

I will give that a shot and let it set in my brain. Variables in models and params in controllers.

=====================
Sam G.

Re: How to use will_paginate on this code?

I'm not sure now if paginate could handle the custom find. I get a MYSQL error now from it.

def with_categorized_listing(page)
  directories.paginate(:all, :include => :categorizations, :joins => 'LEFT JOIN companies ON companies.id = directories.company_id', :order => 'directories.featured DESC',:per_page => 10, :page => page) 
end

Why is it doing an inner join and a left outer join for the same table (categorizations) and totally ignored the companies table from the custom find? It works fine if I use a regular find()

ActiveRecord::StatementInvalid in CategoriesController#show

Mysql::Error: #42000Not unique table/alias: 'categorizations': SELECT count(DISTINCT directories.id) AS count_all FROM directories  LEFT OUTER JOIN categorizations ON categorizations.directory_id = directories.id  INNER JOIN categorizations ON directories.id = categorizations.directory_id    WHERE (( (categorizations.category_id = 6) ) AND ( (categorizations.category_id = 6) ))

RAILS_ROOT: config/..
Application Trace | Framework Trace | Full Trace

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract_adapter.rb:128:in `log'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:243:in `execute'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:399:in `select'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/database_statements.rb:13:in `select_one'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/database_statements.rb:19:in `select_value'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/calculations.rb:212:in `execute_simple_calculation'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/calculations.rb:121:in `calculate'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/calculations.rb:117:in `catch'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/calculations.rb:117:in `calculate'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/calculations.rb:45:in `count'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_many_through_association.rb:110:in `send'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_many_through_association.rb:110:in `method_missing_without_paginate'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:946:in `with_scope'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_many_through_association.rb:110:in `method_missing_without_paginate'
vendor/plugins/will_paginate/lib/will_paginate/finder.rb:82:in `method_missing'
vendor/plugins/will_paginate/lib/will_paginate/finder.rb:120:in `wp_count!'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:946:in `with_scope'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_many_through_association.rb:110:in `send'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_many_through_association.rb:110:in `method_missing_without_paginate'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:946:in `with_scope'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_many_through_association.rb:110:in `method_missing_without_paginate'
vendor/plugins/will_paginate/lib/will_paginate/finder.rb:82:in `method_missing'
vendor/plugins/will_paginate/lib/will_paginate/finder.rb:125:in `wp_count!'
vendor/plugins/will_paginate/lib/will_paginate/finder.rb:89:in `method_missing'
app/models/category.rb:57:in `with_categorized_listing'
app/controllers/categories_controller.rb:21:in `show'

Last edited by shajused (2007-08-20 19:35:18)

=====================
Sam G.

Re: How to use will_paginate on this code?

Hmm, I'm not sure. If a normal find works but the "paginate" method doesn't, then you may want to open a ticket on the will_paginate tracker. Or it may just be that the paginate method doesn't support the :joins option? I don't know.

Railscasts - Free Ruby on Rails Screencasts

Re: How to use will_paginate on this code?

Well after playing around in the console, I got it right and seemingly really VERY simple. Active Record associations to the rescue!!!

From this:

def with_categorized_listing(page)
  directories.find(:all, :include => :categorizations, :joins => 'LEFT JOIN companies ON companies.id = directories.company_id', :order => 'directories.featured DESC')
end

To this:

def with_categorized_listing(page)
  directories.paginate(:all, :order => 'directories.featured DESC',:per_page => 5, :page => page)
end

Thought I'd share in case someone else run into some convoluted custom find and is trying to slap on will_paginate on it.

Lesson learned: Keep it simple and Active Record does his job 99% of the time.

Last edited by shajused (2007-08-27 06:18:27)

=====================
Sam G.