# Newbie: 'find' across multiple relationships

**URL:** <https://www.ruby-forum.com/t/newbie-find-across-multiple-relationships/55697>\
**Category:** Rails\
**Created:** [March 23, 2006, 2:07am UTC](https://www.ruby-forum.com/t/newbie-find-across-multiple-relationships/55697 "2006-03-23T02:07:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 23, 2006, 2:07am UTC](https://www.ruby-forum.com/t/newbie-find-across-multiple-relationships/55697/1 "2006-03-23T02:07:49Z")

</div>

For security purposes, I often want to find objects that are ‘visible’.

e.g.

class Person \< ActiveRecord::Base  
has\_many :x  
end

…

@person.x.find(params[:id]) as opposed to X.find([params[:id]). Simple  
enough for me.

My question:

Say we have -

class Person \< ActiveRecord::Base  
has\_many :x, :class\_name ‘Z’, …  
has\_many :y, :class\_name ‘Z’, …  
end

In other words, two different associations for the same type of model.  
Maybe the second collection is a habtm, etc.

What is the best technique for doing a find on the union of those  
collections? I can think of a half dozen ways, but none of them seem  
‘idiomatic’.

Thanks

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 23, 2006, 1:56pm UTC](https://www.ruby-forum.com/t/newbie-find-across-multiple-relationships/55697/2 "2006-03-23T13:56:58Z")

</div>

Am Donnerstag, den 23.03.2006, 02:07 +0100 schrieb John S.:

> @person.x.find(params[:id]) as opposed to X.find([params[:id]). Simple
> 
> In other words, two different associations for the same type of model.  
> Maybe the second collection is a habtm, etc.
> 
> What is the best technique for doing a find on the union of those  
> collections? I can think of a half dozen ways, but none of them seem  
> ‘idiomatic’.

What about:

@person.x.find(params[:id]) || @person.y.find(params[:id])

–  
Norman T.

[http://blog.inlet-media.de](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 23, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/newbie-find-across-multiple-relationships/55697/3 "2006-03-23T16:42:23Z")

</div>

Norman T. wrote:

> Am Donnerstag, den 23.03.2006, 02:07 +0100 schrieb John S.:
> 
> > @person.x.find(params[:id]) as opposed to X.find([params[:id]). Simple
> > 
> > In other words, two different associations for the same type of model.  
> > Maybe the second collection is a habtm, etc.
> > 
> > What is the best technique for doing a find on the union of those  
> > collections? I can think of a half dozen ways, but none of them seem  
> > ‘idiomatic’.
> 
> What about:
> 
> @person.x.find(params[:id]) || @person.y.find(params[:id])
> 
> –  
> Norman T.
> 
> [http://blog.inlet-media.de](http://blog.inlet-media.de)

Doesn’t find throw RecordNotFound (as opposed to returning nil or an  
empty collection)?.

In any case, I don’t think it would produce the desired effect as I want  
a collection that contains all the results from both associations in the  
case where both have results.

Right now I’ve just defined a method in the model that uses a custom  
conditional to find to OR the different possibilities. Maybe that’s a  
reasonable approach.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 23, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/newbie-find-across-multiple-relationships/55697/4 "2006-03-23T16:59:33Z")

</div>

Am Donnerstag, den 23.03.2006, 16:42 +0100 schrieb John S.:

> > What about:
> > 
> > @person.x.find(params[:id]) || @person.y.find(params[:id])

> Doesn’t find throw RecordNotFound (as opposed to returning nil or an  
> empty collection)?.
> 
> In any case, I don’t think it would produce the desired effect as I want  
> a collection that contains all the results from both associations in the  
> case where both have results.

You are right:

(@person.x.find(params[:id]) rescue nil) || @person.y.find(params[:id])

–  
Norman T.

[http://blog.inlet-media.de](http://blog.inlet-media.de)
