Forum: Ruby on Rails Paginate() from multiple tables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Doug B. (Guest)
on 2006-05-08 00:41
Hi

I'm wondering if there's a way I can gather data from multiple tables
from the child table by using the paginate method?

I would have done it using select_by_sql but I need it to paginate.

My table structure is:

Records is a child of Date and Account

Date  Account
 |      |
 ^      ^
 Records

I want to be able to print a table using the Records controller that
will display child records dependent on a specific date/account.  So I
need a condition from more than one table.

Can anybody help?
Paul B. (Guest)
on 2006-05-08 18:34
(Received via mailing list)
I would assume you could use the :joins option of the paginate method to
get
the tables you want.  I'm not sure I understand you data structure
though.
Do you have a Record object that has a date_id property that is a
foreign
key to Date.id and an account_id that is a foreign key to Account.id?
So
then I think this would do it:

@record_pages, @records = paginate :records, :per_page => 10,
  :joins => ["dates","accounts"],
    :conditions => ["dates.id = ? and accounts.id = ? ", date_id,
account_id]
Ashiro (Guest)
on 2006-05-09 12:32
Paul B. wrote:
> I would assume you could use the :joins option of the paginate method to
> get
> the tables you want.  I'm not sure I understand you data structure
> though.
> Do you have a Record object that has a date_id property that is a
> foreign
> key to Date.id and an account_id that is a foreign key to Account.id?
> So
> then I think this would do it:
>
> @record_pages, @records = paginate :records, :per_page => 10,
>   :joins => ["dates","accounts"],
>     :conditions => ["dates.id = ? and accounts.id = ? ", date_id,
> account_id]

Fantastic.  You're an absolute star.  That works great - thank you.
juantomas (Guest)
on 2006-05-16 12:12
(Received via mailing list)
Doug B. <doug.bromley@...> writes:

>
> Hi
>
> I'm wondering if there's a way I can gather data from multiple tables
> from the child table by using the paginate method?
>

Hi Doug,

did you get the solution for that? I'm with the same problem.
juantomas (Guest)
on 2006-05-16 12:58
(Received via mailing list)
Doug B. <doug.bromley@...> writes:

>
> Hi
>
> I'm wondering if there's a way I can gather data from multiple tables
> from the child table by using the paginate method?
>
look up this link:

http://tinyurl.com/juec9

I will try the :include option to try to solve it.
That's only work for rails 1.1.

# Just 1 database query for all of this:
  authors = Author.find(:all, :include => [
    { :posts => :comments }, :categorizations ])

  authors[0].posts[0].comments[0].body # => "Rock on Rails!"
  authors[0].categorizations[0].name   # => "Less software"
This topic is locked and can not be replied to.