# Find for multiple tables & updates

**URL:** <https://www.ruby-forum.com/t/find-for-multiple-tables-updates/129610>\
**Category:** Rails\
**Created:** [February 20, 2008, 2:45am UTC](https://www.ruby-forum.com/t/find-for-multiple-tables-updates/129610 "2008-02-20T02:45:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kenwei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e274bd/32.png) [@kenwei](https://www.ruby-forum.com/u/kenwei)\
**Post date:** [February 20, 2008, 2:45am UTC](https://www.ruby-forum.com/t/find-for-multiple-tables-updates/129610/1 "2008-02-20T02:45:06Z")

</div>

Would like to use ActiveRecord find for multiple table lookups with sort  
order on parent table.

Example: Item table belongs to Category and Section tables. Category and  
Section tables have only 2 attributes: id, text field (category and  
section). Item has many attributes in addition to category\_id &  
section\_id.

```
1. Sort items by category or section TEXT field not the id fields.
2. Be able to update the Item attributes but not category or

```

section.

A. Use find\_by\_sql?  
B. Use find and “:include” option.  
C. Use “:from” option to refer to an SQL view of the joined tables?  
i. Then switch to an updatable Item form?  
D. Use some other approach?

Best regards,

Ken W.  
Sugar Creek, Missouri  
The “Show Me” state.

---

<div class="post-metadata">

**Author:** ![kenwei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e274bd/32.png) [@kenwei](https://www.ruby-forum.com/u/kenwei)\
**Post date:** [February 20, 2008, 8:13pm UTC](https://www.ruby-forum.com/t/find-for-multiple-tables-updates/129610/2 "2008-02-20T20:13:14Z")

</div>

Go with B.

Items.find(:all, :include=\>[:category, :section],  
:order=\>“categories.name,  
sections.name, items.name”)

The key is the :order option.

On Feb 19, 8:45 pm, Ken W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kenwei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e274bd/32.png) [@kenwei](https://www.ruby-forum.com/u/kenwei)\
**Post date:** [February 21, 2008, 2:03am UTC](https://www.ruby-forum.com/t/find-for-multiple-tables-updates/129610/3 "2008-02-21T02:03:34Z")

</div>

AndyV wrote:

> Go with B.
> 
> Items.find(:all, :include=\>[:category, :section],  
> :order=\>“categories.name,  
> sections.name, items.name”)
> 
> The key is the :order option.
> 
> On Feb 19, 8:45 pm, Ken W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Works like a charm! A little tweaking and the rails magic delights once  
again.

Rails is extraordinarily amazing. (Once you know how it works. 🙂

If only the documentation and examples were a bit more illuminating.  
Sigh.

Many thanks, Andy.

---

<div class="post-metadata">

**Author:** ![kenwei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e274bd/32.png) [@kenwei](https://www.ruby-forum.com/u/kenwei)\
**Post date:** [February 21, 2008, 5:20am UTC](https://www.ruby-forum.com/t/find-for-multiple-tables-updates/129610/4 "2008-02-21T05:20:42Z")

</div>

Ken W. wrote:

> AndyV wrote:
> 
> If only the documentation and examples were a bit more illuminating.  
> Sigh.
> 
> Many thanks, Andy.

Offtopic but I /sign this “petition”.

🙂
