Will_paginate and :conditions or :select with associations

Hi you all,
I’m using Will_paginate plugin for paginating my results, and it worked
fine with just one model, even adding the :select param, such as:

select_statement = "DISTINCT " + @att
@bugs = Bug.paginate(:page => params[:page], :per_page=>30, :select =>
select_statement, :order => @att)

The problem is when I do something like:
@client.bugs.paginate(:page => params[:page], :per_page=>30, :select =>
select_statement, :order => @att)

It’s the same, but getting the bugs of one client. And this does not
work.
The things in “select”, or in “conditions”, are inexistent for
will_paginate (i see in the log file the select clause does not contain
anything of what is stated in “select” or “conditions” params.

Any ideas? Thanks

paginate is a class method of ActiveRecord::Base, not class Array.
That’s
why @client.bugs failed, i think.

You can use :join/:include to achieve your goal.

On Jan 18, 2008 11:08 PM, Damaris F.
[email protected]

Sorry but, where do I have to use the join and include, in the paginate
method? ( I suppose…:S)

However, if so, the “will_paginate” plugin is very restrictive. I mean,
IMHO, it’s very common to paginate results from associations. I will
also look for another plugin to paginate.

On Jan 19, 2008 11:23 PM, Damaris F.
[email protected]
wrote:

Sorry but, where do I have to use the join and include, in the paginate
method? ( I suppose…:S)

yes. the similar usage like find.

for example,

Model.pagindate :include => [], :conditions =>[], :group => xx, :order
=>
xx, :page => xx.

Very simple, isn’t it? :slight_smile: Hope it’s helpful.

On Jan 21, 2008 1:06 AM, Damaris F.
[email protected]
wrote:

Client.find_by_dirip(self.diripa)
conditions… how could I with will_paginate? :frowning:
Bug.paginate :include => [:client], :conditions => [‘client_id = ?’,
client.id]

Um, but the “join” and “include” methods makes references to
“belongs_to” associations and so, am I correct?

The problem is my associations are not these ones, I mean, I don’t have
the associations define with belongs_to and has_many and so. My models
are:

class Bug < ActiveRecord::Base
def client
Client.find_by_dirip(self.diripa)
end
end

class Client < ActiveRecord::Base
def bugs
Bug.find_all_by_diripa(self.dirip)
end
end

So… if I want to paginate the Bugs of one Client with certain
conditions… how could I with will_paginate? :frowning:

Thanks.

sishen wrote:

On Jan 19, 2008 11:23 PM, Damaris F.
[email protected]
wrote:

Sorry but, where do I have to use the join and include, in the paginate
method? ( I suppose…:S)

yes. the similar usage like find.

for example,

Model.pagindate :include => [], :conditions =>[], :group => xx, :order
=>
xx, :page => xx.

Very simple, isn’t it? :slight_smile: Hope it’s helpful.

But this is what I am doing now to do the fix, but this is not DRY
anymore.
@values = Bug.paginate(:page => params[:page], :per_page=>30, :select =>
select_statement, :conditions =>[“diripa = ?”, @client.dirip])

I have the client.bugs method in my model, I would not have to do this.
I didn’t have to do this with the classic pagination. Gr :frowning:

sishen wrote:

On Jan 21, 2008 1:06 AM, Damaris F.
[email protected]
wrote:

Client.find_by_dirip(self.diripa)
conditions… how could I with will_paginate? :frowning:
Bug.paginate :include => [:client], :conditions => [‘client_id = ?’,
client.id]

Can you accept this?

class Client < ActiveRecord::Base
has_many :bugs do
def paginate(options)
Bug.paginate
end
end
end

On Jan 21, 2008 3:02 AM, Damaris F.
[email protected]
wrote:

But this is what I am doing now to do the fix, but this is not DRY
anymore.
@values = Bug.paginate(:page => params[:page], :per_page=>30, :select =>
select_statement, :conditions =>[“diripa = ?”, @client.dirip])

I have the client.bugs method in my model, I would not have to do this.
I didn’t have to do this with the classic pagination. Gr :frowning:

I already forgot how to use classic pagination yet… -,-

Um, no. The problem is that I cannot use the default “has_many”
association. I have to state my own “bugs” method cause I’m working with
a legacy database, and the primary and foreign key columns are not using
the Rails convention.

This is why I have something like:
class Client < ActiveRecord::Base
def bugs
Bug.find_all_by_diripa(self.dirip)
end
end

Perhaps will_paginate would work fine if I used conventional
associations, but I must do my own methods (in fact, I post a thread
with no useful answers with this keys problem [1]).

Thanks for your patience.

[1] Customizing belongs_to method - Rails - Ruby-Forum

sishen wrote:

Can you accept this?

class Client < ActiveRecord::Base
has_many :bugs do
def paginate(options)
Bug.paginate
end
end
end

On Jan 21, 2008 3:02 AM, Damaris F.
[email protected]
wrote:

But this is what I am doing now to do the fix, but this is not DRY
anymore.
@values = Bug.paginate(:page => params[:page], :per_page=>30, :select =>
select_statement, :conditions =>[“diripa = ?”, @client.dirip])

I have the client.bugs method in my model, I would not have to do this.
I didn’t have to do this with the classic pagination. Gr :frowning:

I already forgot how to use classic pagination yet… -,-