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 -- Posted via http://www.ruby-forum.com/.
Norman Timmler
2006-Mar-23 12:56 UTC
[Rails] Newbie: ''find'' across multiple relationships
Am Donnerstag, den 23.03.2006, 02:07 +0100 schrieb John Smith:> 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''.What about: @person.x.find(params[:id]) || @person.y.find(params[:id]) -- Norman Timmler http://blog.inlet-media.de
John Smith
2006-Mar-23 15:47 UTC
[Rails] Re: Newbie: ''find'' across multiple relationships
Norman Timmler wrote:> Am Donnerstag, den 23.03.2006, 02:07 +0100 schrieb John Smith: >> @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 Timmler > > http://blog.inlet-media.deDoesn''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. -- Posted via http://www.ruby-forum.com/.
Norman Timmler
2006-Mar-23 15:59 UTC
[Rails] Re: Newbie: ''find'' across multiple relationships
Am Donnerstag, den 23.03.2006, 16:42 +0100 schrieb John Smith:> Norman Timmler wrote: > > Am Donnerstag, den 23.03.2006, 02:07 +0100 schrieb John Smith: > >> @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])> 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 Timmler http://blog.inlet-media.de