Paginate Not Including Other Tables w/ :include?


#1

I am trying to use pagniate as follows:

@merch_pages, @merch = paginate :merch_item, :per_page => 15, :include
=>
:merch_formats, :conditions => [“active_status = 1 AND format_name = ?”,
params[:id]]

My ‘merch_item’ model contains a ‘has_many :merch_formats’.

active_status and format_name are defined on merch_format, not
merch_item.

The problem is that the SQL generated isn’t including merch_formats, it
is
simply outputting:

SELECT COUNT(*) FROM merch_items WHERE (active_status = 1 AND
format_name =
‘Sweatshirt’)

This of course fails, as the required JOIN wasn’t inserted.

Based on the paginate documentation, it seems that :include should work
the
same way that it does during a normal find.

What am I doing wrong?

Thanks,
Hunter


#2

What does the relationship from :merch_item and merch_formats look like
?

I have a similar problem last week but my models objects were not
properly configured…


#3

class MerchItem < ActiveRecord::Base
has_many :merch_formats, :dependent => true
end

class MerchFormat < ActiveRecord::Base
belongs_to :merch_item
end


#4

I just created a new rails plugin (
http://wiki.rubyonrails.com/rails/pages/Plugins#countlimit) that makes
:include work with paginate. This was enabled by changing Model.count
so
that it now accepts a hash of options including: :include and
:conditions.
This new Model.count is backwards compatible with the existing
Model.countin ActiveRecord, and it passes all Active Record unit tests
using MySQL and
Postgres.

Check it out if you want to use eager loading with paginate… and
please
send any feedback my way!

You can get the plugin from:

svn://jthopple.com/public/rails/plugins/count_limit_associations

Thanks,
Jeremy


#5

Shouldn¹t something like this be in the core?

It seems almost like a bug with Model.count if it can¹t support all the
options that the items that use it need…?

From: Jeremy H. removed_email_address@domain.invalid
Reply-To: removed_email_address@domain.invalid
Date: Mon, 12 Dec 2005 13:15:01 -0500
To: removed_email_address@domain.invalid
Subject: Re: [Rails] Paginate Not Including Other Tables w/ :include?

I just created a new rails plugin
(http://wiki.rubyonrails.com/rails/pages/Plugins#countlimit) that makes
:include work with paginate. This was enabled by changing Model.count
so
that it now accepts a hash of options including: :include and
:conditions.
This new Model.count is backwards compatible with the existing
Model.count
in ActiveRecord, and it passes all Active Record unit tests using MySQL
and
Postgres.

Check it out if you want to use eager loading with paginate… and
please
send any feedback my way!

You can get the plugin from:

svn://jthopple.com/public/rails/plugins/count_limit_associations

Thanks,
Jeremy