# Locating Objects by associated object.properties

**URL:** <https://www.ruby-forum.com/t/locating-objects-by-associated-object-properties/52416>\
**Category:** Rails\
**Created:** [January 13, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/locating-objects-by-associated-object-properties/52416 "2006-01-13T21:37:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![krat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@krat](https://www.ruby-forum.com/u/krat)\
**Post date:** [January 13, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/locating-objects-by-associated-object-properties/52416/1 "2006-01-13T21:37:25Z")

</div>

I am just getting the hang of this, so forgive me if this is a stupid  
question…

- I have two models/tables: Owners and Cars
- Owners may have many cars. Cars have one owner.
- Cars have a property named Color.

Question: how do I get a list of all the Owners who have RED cars?

I want something like this to work:  
Owner.find\_by\_car.color(“RED”)

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![krat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@krat](https://www.ruby-forum.com/u/krat)\
**Post date:** [January 13, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/locating-objects-by-associated-object-properties/52416/2 "2006-01-13T22:01:19Z")

</div>

# find the owner with id = 3

@owner = Owner.find(3)

# find all cars that belong to this owner AND have a red color

@red\_cars = @owner.cars.find(:all, :conditions =\> [‘color = ?’, ‘red’])

I think this works.

Jules

---

<div class="post-metadata">

**Author:** ![krat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@krat](https://www.ruby-forum.com/u/krat)\
**Post date:** [January 13, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/locating-objects-by-associated-object-properties/52416/3 "2006-01-13T22:05:25Z")

</div>

Whoops, this was not what you meant…

Owner.find\_by\_sql(‘SELECT owners.\* FROM owners, cars WHERE cars.color =  
“red” AND cars.owner\_id = owners.id’)

---

<div class="post-metadata">

**Author:** ![krat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@krat](https://www.ruby-forum.com/u/krat)\
**Post date:** [January 13, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/locating-objects-by-associated-object-properties/52416/4 "2006-01-13T22:12:50Z")

</div>

Thanks Jules

I guess I was wondering if there was a design-pattern for things like  
this that do not go all the way down to the SQL.

-KRat

---

<div class="post-metadata">

**Author:** ![krat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@krat](https://www.ruby-forum.com/u/krat)\
**Post date:** [January 14, 2006, 2:41am UTC](https://www.ruby-forum.com/t/locating-objects-by-associated-object-properties/52416/5 "2006-01-14T02:41:37Z")

</div>

How about:

owner  
has\_many :cars

car  
belongs\_to :owner

red\_car\_owners = Owner.find(  
:all,  
:include =\> :cars,  
:conditions =\> [“cars.colour = ?”, ‘RED’]  
)

where “cars.colour” is referring to the cars table directly.

Try tailing the development log to get a peek at the left outer join  
queries  
put together by the :include eager loading and you’ll see that the cars  
table  
is included in the Owner.find query. You can then add conditions which  
reference the cars table. I think…

On Saturday 14 January 2006 6:12 am, keith raterink wrote:

> Thanks Jules
> 
> I guess I was wondering if there was a design-pattern for things like  
> this that do not go all the way down to the SQL.
> 
> -KRat

–

Mark B.  
Easy Schedule Management  
[http://easy-online-schedule.com](http://easy-online-schedule.com)

---

<div class="post-metadata">

**Author:** ![krat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@krat](https://www.ruby-forum.com/u/krat)\
**Post date:** [January 14, 2006, 4:44am UTC](https://www.ruby-forum.com/t/locating-objects-by-associated-object-properties/52416/6 "2006-01-14T04:44:55Z")

</div>

hot damn mark!

I will give it a try…it sure seems to make sense!

THANKS  
-krat

---

<div class="post-metadata">

**Author:** ![krat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@krat](https://www.ruby-forum.com/u/krat)\
**Post date:** [January 14, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/locating-objects-by-associated-object-properties/52416/7 "2006-01-14T14:15:14Z")

</div>

No worries, hope it helps.

cheers,  
mark

On Saturday 14 January 2006 12:44 pm, keith raterink wrote:

> hot damn mark!
> 
> I will give it a try…it sure seems to make sense!
> 
> THANKS  
> -krat

–

Mark B.  
Easy Schedule Management  
[http://easy-online-schedule.com](http://easy-online-schedule.com)
