Hello,
I know how to find all the associated objects in a polymorphic
relationship, but how do you find a particular associated object ?
Currently, I have a method that collects all the associated objects.
Then, to find a particular object in that collection I search on id and
type. See the code below. I am wondering if there is a better way?
I have read the ActiveRecord API and no STI is not what I want. As a
suggestion to you, please read up on Polymorphic tables. Your
suggestion still does not give me what I am looking for. Using
user.resources.find(...) returns a Resource. Thanks anyways.
Anyone else have any suggestions. I posted a solution in my original
post and am really just trying to improve it. Here it is again:
#method to get all the associated objects - returns an array of the
user's Polymorphic objects
def modules
resources.collect { |a| a.resource }
end
#find a particular associated resource by searching, by id and type,
through the list of polymorphic objects
def find_mod(id, type)
mods = self.modules
mods.each do |m|
if m.id == id and m.type.to_s == type
return m
end
end
end