# Basic Active Record find question

**URL:** <https://www.ruby-forum.com/t/basic-active-record-find-question/67950>\
**Category:** Rails\
**Created:** [September 8, 2006, 3:32am UTC](https://www.ruby-forum.com/t/basic-active-record-find-question/67950 "2006-09-08T03:32:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cocoa\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a87d85/32.png) [@Cocoa\_G](https://www.ruby-forum.com/u/Cocoa_G)\
**Post date:** [September 8, 2006, 3:32am UTC](https://www.ruby-forum.com/t/basic-active-record-find-question/67950/1 "2006-09-08T03:32:38Z")

</div>

Given the following simple model:

A page has many sections  
A section has many entries  
Each entry has a time\_stamp

I can easily find all the entries of a section sorted by time\_stamp:  
section.entries.find (:all, :order =\> “time\_stamp”)

But what if I need all the entries of a page sorted by time\_stamp?

One thought was to search across all entries and use Array.include? to  
constrain the entries to the sections of a page:  
section\_ids = []  
page.sections.each do |section|  
section\_ids \<\< section.id  
end  
Section.find(:all, :conditions =\> ["?.include? section\_id",  
section\_ids],  
:order =\> “time\_stamp”)

I can’t get this to work because I can’t find a way to escape the second  
question mark.

But perhaps there is a better way of doing this?

Cheers!

---

<div class="post-metadata">

**Author:** ![Cocoa\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a87d85/32.png) [@Cocoa\_G](https://www.ruby-forum.com/u/Cocoa_G)\
**Post date:** [September 8, 2006, 7:49am UTC](https://www.ruby-forum.com/t/basic-active-record-find-question/67950/2 "2006-09-08T07:49:03Z")

</div>

On Sep 7, 2006, at 6:58 PM, Cocoa G. wrote:

> section\_ids = []  
> page.sections.each do |section|  
> section\_ids \<\< section.id  
> end  
> Section.find (:all, :conditions =\> ["?.include? section\_id",  
> section\_ids], :order =\> “time\_stamp”)

Try this:  
section\_ids = page.sections.map {|s| s.id}  
Entries.find (:all, :conditions =\> “section\_id in (#{section\_ids \*  
‘,’})”, :order =\> “time\_stamp”)

Aaron

---

<div class="post-metadata">

**Author:** ![Cocoa\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a87d85/32.png) [@Cocoa\_G](https://www.ruby-forum.com/u/Cocoa_G)\
**Post date:** [September 8, 2006, 9:34pm UTC](https://www.ruby-forum.com/t/basic-active-record-find-question/67950/3 "2006-09-08T21:34:58Z")

</div>

On 9/7/06, Aaron B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Try this:  
> section\_ids = page.sections.map {|s| s.id}  
> Entries.find (:all, :conditions =\> “section\_id in (#{section\_ids \* ‘,’})”,  
> :order =\> “time\_stamp”)

Thank you.

For my fellow Ruby newbies, section\_ids \* ‘,’ expands to a string with  
each  
element of the section\_ids array and a ‘,’ in between: “1,2,3”  
#{} allows substituting the value of any Ruby code into a string. The  
net  
effect is a condition such as ‘section\_id in (1,2,3)’

Cheers!

---

<div class="post-metadata">

**Author:** ![Cocoa\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a87d85/32.png) [@Cocoa\_G](https://www.ruby-forum.com/u/Cocoa_G)\
**Post date:** [September 9, 2006, 2:23am UTC](https://www.ruby-forum.com/t/basic-active-record-find-question/67950/4 "2006-09-09T02:23:26Z")

</div>

On 9/8/06, Jeremy K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Simpler:  
> Entries.find(:all, :conditions =\> [‘section\_id in (?)’, section\_ids]

This makes sense and it works well.

Simplest:

> page.sections.find(:all, :include =\> :entries)

This won’t work in this case because I need the entries sorted by  
time\_stamp  
across all sections, so I’d have to iterate through the returned  
sections,  
store all the entries and then sort them myself. Doing an Entry.find  
along  
with :order makes the database do the sorting for me.

Cheers!

---

<div class="post-metadata">

**Author:** ![Cocoa\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a87d85/32.png) [@Cocoa\_G](https://www.ruby-forum.com/u/Cocoa_G)\
**Post date:** [September 8, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/basic-active-record-find-question/67950/5 "2006-09-08T23:09:08Z")

</div>

On 9/8/06, Cocoa G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Section.find (:all, :conditions =\> [“?.include? section\_id”,  
> > For my fellow Ruby newbies, section\_ids \* ‘,’ expands to a string with  
> > each element of the section\_ids array and a ‘,’ in between: “1,2,3”  
> > #{} allows substituting the value of any Ruby code into a string. The net  
> > effect is a condition such as ‘section\_id in (1,2,3)’

Simpler:  
Entries.find(:all, :conditions =\> [‘section\_id in (?)’, section\_ids]

Simplest:  
page.sections.find(:all, :include =\> :entries)

jeremy
