Forum: Ruby on Rails Newbie: 'find' across multiple relationships

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.
John S. (Guest)
on 2006-03-23 03:07
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
Norman T. (Guest)
on 2006-03-23 14:56
(Received via mailing list)
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
John S. (Guest)
on 2006-03-23 17:42
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


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.
Norman T. (Guest)
on 2006-03-23 17:59
(Received via mailing list)
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
This topic is locked and can not be replied to.