# Pagination

**URL:** <https://www.ruby-forum.com/t/pagination/107919>\
**Category:** Rails\
**Created:** [June 27, 2007, 1:54pm UTC](https://www.ruby-forum.com/t/pagination/107919 "2007-06-27T13:54:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@mani](https://www.ruby-forum.com/u/mani)\
**Post date:** [June 27, 2007, 1:54pm UTC](https://www.ruby-forum.com/t/pagination/107919/1 "2007-06-27T13:54:52Z")

</div>

hi all… iam new to rails…

iam developing a app and unable to solve one problem…the problem is  
regarding pagination…

im my app in controller class i have these two methods:

i have two tables one is chatusers and the other is  
voicechats…chatusers foreign key refers voicechats…the  
below methods are present in chatuser controller…

def create  
@chatuser = Chatuser.new(params[:chatuser])

```
if Voicechat.find(@chatuser.voicechat_id).password ==

```

@chatuser.password  
if @chatuser.save  
flash[:notice] = ‘Sign in Successfull…’  
@chatusers = Chatuser.find(:all, :conditions =\> [“voicechat\_id  
= @chatuser.voicechat\_id”])  
redirect\_to :action=\> ‘list’  
else  
render :action =\> ‘new’  
end  
else  
flash[:notice]=‘Plz enter correct password.’  
render :action =\> ‘new’

end  
end

chatusers is a table in the database…

def list  
@chatuser\_pages, @chatusers=paginate( :chatusers, :conditions=\>["  
voicechat\_id = @chatuser.voicechat\_id "],:per\_page=\>18)  
end

chatusers is a table in the database…in which foreign key refers to  
voicechats table…

my goals:

1. wen create action is taking place, only chatusers with that foreign  
key (which is assigned to the new user) should be listed… for  
that i have edited the default list method…using conditions but it  
isnt working…instead it is not showing anything…even though some  
records are present in chatusers table wid that foreign  
key…

So plz anyone help me out…thanks in advance…

---

<div class="post-metadata">

**Author:** ![mani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@mani](https://www.ruby-forum.com/u/mani)\
**Post date:** [June 29, 2007, 7:33am UTC](https://www.ruby-forum.com/t/pagination/107919/2 "2007-06-29T07:33:32Z")

</div>

hi mike…  
thank u for ur valuable reply…

wen i wrote this line of code:

@chatuser\_pages, @chatusers  
=paginate(:chatusers, :conditions=\>[“voicechat\_id=?”,  
@chatuser.voicechat\_id],:per\_page=\>18) in the list method it is  
showing following error:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.each

And wen i copy this code code and paste it in the create method and i  
also renamed the list(int views folder) file to create…it is working  
and it is showing wat is required…i donno y it is not showing the  
in the first way…

In create method… there is a line called if @chatuser.save it  
means it is saving the record in the model… and after that it is  
probabaly set to null automatically… and may be thats y we were  
unable to solve get @chatuser.voicechat\_id.

Plz help me out to overcome this problem…thanks in advance…bye

---

<div class="post-metadata">

**Author:** ![mani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@mani](https://www.ruby-forum.com/u/mani)\
**Post date:** [June 29, 2007, 8:18am UTC](https://www.ruby-forum.com/t/pagination/107919/3 "2007-06-29T08:18:40Z")

</div>

and one more thing… i think @chatuser is local to class and may be  
that y its is not accessible outside…  
can we anyway get the record id that is added recently…bcause if we  
can get that we can replace @chatuser.voicechat\_id  
with Chatuser.find(id).voicechat\_id … plz try to help me  
out …have a nice time bye

---

<div class="post-metadata">

**Author:** ![mani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@mani](https://www.ruby-forum.com/u/mani)\
**Post date:** [June 27, 2007, 10:10pm UTC](https://www.ruby-forum.com/t/pagination/107919/4 "2007-06-27T22:10:53Z")

</div>

On 6/27/07, mani [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> below methods are present in chatuser controller…  
> redirect\_to :action=\> ‘list’  
> chatusers is a table in the database…
> 
> def list  
> @chatuser\_pages, @chatusers=paginate( :chatusers, :conditions=\>["  
> voicechat\_id = @chatuser.voicechat\_id "],:per\_page=\>18)  
> end

Your conditions parameter is wrong… You’re sending :conditions =\>  
[“voicechat\_id = @chatuser.voicechat\_id”] which is exactly what’s  
going to show up in the mysql query, which of course will fail… For  
example, it’ll generate something like:

select \* from chatusers where voicechat\_id = @chatuser.voicechat\_id …

which isn’t valid sql… You want to use the following:

:conditions =\> [“voicechat\_id = ?”, @chatuser.voicechat\_id]

which will bind the @chatuser.voicechat\_id value to the ? placeholder  
character.

you should also look at the “will\_paginate” plugin  
([http://errtheblog.com/post/4791](http://errtheblog.com/post/4791)), it’s much better than the rails  
built in pagination.

Mike
