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
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
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.
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