# Paginate trouble

**URL:** <https://www.ruby-forum.com/t/paginate-trouble/138420>\
**Category:** Rails\
**Created:** [May 22, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/paginate-trouble/138420 "2008-05-22T21:19:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 22, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/paginate-trouble/138420/1 "2008-05-22T21:19:36Z")

</div>

Well, I added will\_paginate to my application. Though when I attempt to  
use I get an action controller error.

here is the app/controller/community\_controller file I created:

class CommunityController \< ApplicationController  
helper :profile

def index  
@title = “Community”  
@letters = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”.split("")  
if params[:id]  
@initial = params[:id]  
@pages, specs = paginate(:specs,  
:conditions =\> [“last\_name LIKE ?”,  
@initial+"%"],  
:order =\> “last\_name, first\_name”)  
@users = specs.collect { |spec| spec.user }  
end  
end

def browse  
end

def search  
if params[:q]  
query = params[:q]  
# First find the user hits…  
@users = User.find\_by\_contents(query, :limit =\> :all)  
# …then the subhits.  
specs = Spec.find\_by\_contents(query, :limit =\> :all)  
faqs = Faq.find\_by\_contents(query, :limit =\> :all)

```
  # Now combine into one list of distinct users sorted by last name.
  hits = specs + faqs
  @users.concat(hits.collect { |hit| hit.user }).uniq!
  # Sort by last name (requires a spec for each user).
  @users.each { |user| user.spec ||= Spec.new }
  @users = @users.sort_by { |user| user.spec.last_name }
end

```

end  
end

The message is:

NoMethodError in CommunityController#index

undefined method `paginate’ for #CommunityController:0xb6bd4ba4

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 23, 2008, 5:10am UTC](https://www.ruby-forum.com/t/paginate-trouble/138420/2 "2008-05-23T05:10:41Z")

</div>

On Thu, May 22, 2008 at 3:19 PM, Sean S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Well, I added will\_paginate to my application. Though when I attempt to  
> use I get an action controller error.

The message is:

> NoMethodError in CommunityController#index
> 
> undefined method `paginate’ for #CommunityController:0xb6bd4ba4

Did you restart your server?

Brandon

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 23, 2008, 7:18am UTC](https://www.ruby-forum.com/t/paginate-trouble/138420/3 "2008-05-23T07:18:13Z")

</div>

Kieran P wrote:

> Hello,
> 
> I’m still new to Rails, but installed will\_paginate yesterday with no  
> problems. I believe pagination is a method from your model, not the  
> controller. Thus
> 
> @pages, specs = paginate(:specs,
> 
> should become
> 
> @pages, specs = Community.paginate(:specs,
> 
> or whatever the model is.
> 
> Hope this helps.
> 
> Regards  
> k776
> 
> On Fri, May 23, 2008 at 7:19 AM, Sean S.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

When I change that it throws the error:

NameError in CommunityController#index

undefined local variable or method `spec’ for  
#CommunityController:0xb6b0d504

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 23, 2008, 5:39am UTC](https://www.ruby-forum.com/t/paginate-trouble/138420/4 "2008-05-23T05:39:54Z")

</div>

Hello,

I’m still new to Rails, but installed will\_paginate yesterday with no  
problems. I believe pagination is a method from your model, not the  
controller. Thus

@pages, specs = paginate(:specs,

should become

@pages, specs = Community.paginate(:specs,

or whatever the model is.

Hope this helps.

Regards  
k776

On Fri, May 23, 2008 at 7:19 AM, Sean S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 23, 2008, 8:28am UTC](https://www.ruby-forum.com/t/paginate-trouble/138420/5 "2008-05-23T08:28:42Z")

</div>

Rails T. wrote:

> > ```
> > @pages, specs = paginate(:specs,
> > :conditions => ["last_name LIKE ?",
> > 
> > ```
> > 
> > @initial+“%”],  
> > :order =\> “last\_name, first\_name”)  
> > @users = specs.collect { |spec| spec.user }
> 
> It is confusing. because your ‘specs’ only can be accessed in controller  
> not in view. what is variable do you use for pagination? ‘specs’ or  
> ‘@users’?
> 
> Kieran P wrote:
> 
> > @pages, specs = Community.paginate(:specs,
> 
> you can not use it because :specs is different table name than of  
> community.  
> @pages, @specs = Spec.paginate :conditions =\> [“last\_name LIKE ?”,  
> @initial+“%”], :order =\> “last\_name, first\_name”, :page =\> params[:page]
> 
> Reinhart  
> [http://teapoci.blogspot.com](http://teapoci.blogspot.com)

I believe it is tied in with user.

This is the app/views/community/\_user\_table.rhtml file:

\<% if @users and not @users.empty? %\>

 \<% @users.each do |user| %\> \<% end %\> \<% if paginated? %\> \<% end %\>

| Name | Age | Gender | Location |
| --- | --- | --- | --- |
| \<%= link\_to user.name, profile\_for(user) %\> | \<%= user.spec.age %\> | \<%= user.spec.gender %\> | \<%= user.spec.location %\> |
| Pages: \<%= pagination\_links(@pages, :params =\> params) %\> |

\<% end %\>

If that helps answer your question.

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 23, 2008, 9:07am UTC](https://www.ruby-forum.com/t/paginate-trouble/138420/6 "2008-05-23T09:07:28Z")

</div>

hmmm… now i understand what you want to do.

yes, of course your way is wrong. Try it :

@spec = Spec.find(:all, :conditions =\> [“last\_name LIKE  
?”,@initial+“%”], :order =\> “last\_name, first\_name”)

@user\_collection = @spec.collect { |spec| spec.user }

@pages, @users = @user\_collection.paginate :page =\> params[:page]

I read here:

> <https://github.com/mislav/will_paginate/tree/master/lib/will_paginate>
>
> //github.com/mislav/will\_paginate/tree/master/lib/will\_paginate

This will\_paginate support Array and Collection pagination or update  
your pagination or copy those file to your  
vendor\plugins\will\_paginate\lib\will\_paginate

or read it : [devchix.com at Directnic](http://www.devchix.com/2007/07/23/will_paginate-array/)

Reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 23, 2008, 9:10am UTC](https://www.ruby-forum.com/t/paginate-trouble/138420/7 "2008-05-23T09:10:52Z")

</div>

I have solution with regular pagination (not will\_paginate)

@spec = Spec.find(:all, :conditions =\> [“last\_name LIKE  
?”,@initial+“%”], :order =\> “last\_name, first\_name”)

@user\_collection = @spec.collect { |spec| spec.user }

@pages, @users = paginate\_collection @user\_collection, :page =\>  
@params[:page]

private

def paginate\_collection(collection, options = {})  
default\_options = {:per\_page =\> 10, :page =\> 1}  
options = default\_options.merge options

```
pages = Paginator.new self, collection.size, options[:per_page], 

```

options[:page]  
first = pages.current.offset  
last = [first + options[:per\_page], collection.size].min  
slice = collection[first…last]  
return [pages, slice]  
end

Render From:

> **[Paginate Array Objects in Ruby](http://teapoci.blogspot.com/2008/04/paginate-array-objects-in-ruby.html)**
>
> def paginate\_collection(collection, options = {}) default\_options = {:per\_page =\> 10, :page =\> 1} options = default\_options.me...

Good LUcK,  
Reinhart

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [August 6, 2008, 3:47pm UTC](https://www.ruby-forum.com/t/paginate-trouble/138420/8 "2008-08-06T15:47:03Z")

</div>

Did you sort out this problem?  
I’ve got the same error message!

Regards

Daniel

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 23, 2008, 7:33am UTC](https://www.ruby-forum.com/t/paginate-trouble/138420/9 "2008-05-23T07:33:07Z")

</div>

> ```
> @pages, specs = paginate(:specs,
> :conditions => ["last_name LIKE ?",
> 
> ```
> 
> @initial+“%”],  
> :order =\> “last\_name, first\_name”)  
> @users = specs.collect { |spec| spec.user }

It is confusing. because your ‘specs’ only can be accessed in controller  
not in view. what is variable do you use for pagination? ‘specs’ or  
‘@users’?

Kieran P wrote:

> @pages, specs = Community.paginate(:specs,

you can not use it because :specs is different table name than of  
community.  
@pages, @specs = Spec.paginate :conditions =\> [“last\_name LIKE ?”,  
@initial+“%”], :order =\> “last\_name, first\_name”, :page =\> params[:page]

Reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)
