Sorry if this has been answered a zillion times before: What the best way to code the question "does the associated collection beloning to object a contain object b", given habtm relationship between classes A and B? My problem is that the object.collection.find method only supports the find(id) style. which throws an error if it doesn''t find the potentially associated object in the collection. Do I really have to get into catching errors for this seemingly simple question? -- Robert Jones
Robert Jones wrote:> Sorry if this has been answered a zillion times before: > > What the best way to code the question "does the associated collection > beloning to object a contain object b", given habtm relationship between > classes A and B? > > My problem is that the object.collection.find method only supports the > find(id) style. which throws an error if it doesn''t find the potentially > associated object in the collection. Do I really have to get into catching > errors for this seemingly simple question?I don''t know if it is a good way to do it, but you could do Object.collection.collect {|o| o.id}.include?(id) couldn''t you? Hope that helps, but I''m sure somebody on here can come up with something better. Sean
Robert Jones wrote:> Sorry if this has been answered a zillion times before: > > What the best way to code the question "does the associated collection > beloning to object a contain object b", given habtm relationship between > classes A and B? > > My problem is that the object.collection.find method only supports the > find(id) style. which throws an error if it doesn''t find the potentially > associated object in the collection. Do I really have to get into > catching errors for this seemingly simple question?Double doh! include? does it, as was kindly pointed out to me directly. The scary thing is that I remember getting stuck on this before now! Middle age sucks. -- Robert Jones
Sean Wallace wrote:> Robert Jones wrote: >> Sorry if this has been answered a zillion times before: >> >> What the best way to code the question "does the associated collection >> beloning to object a contain object b", given habtm relationship between >> classes A and B? >> >> My problem is that the object.collection.find method only supports the >> find(id) style. which throws an error if it doesn''t find the potentially >> associated object in the collection. Do I really have to get into >> catching errors for this seemingly simple question? > > I don''t know if it is a good way to do it, but you could do > > Object.collection.collect {|o| o.id}.include?(id) > > couldn''t you? > > Hope that helps, but I''m sure somebody on here can come up with > something better. > > SeanCheers Sean! I already posted my "doh I''m a idiot" reply to the group cos I thought you''d replied directly to me rather than to the group. Anyone got anything better :) -- Robert Jones