Polymorphic table - find one associated object

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?

Models
User

has_many :resources

#method to get all the associated objects
def modules
mods = resources.collect { |a| a.resource }
end

#find a particular associated resource
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

Resource

belongs_to :user
belongs_to :resource, :polymorphic =>true

Thanks in advance – K

Does anyone know? I am sure that other people have needed this type of
functionality.

user.resources.find(id)

Vish

user.resources.find(:all, :conditions => ‘type = “Books” AND id = 1’)

Please go through the ActiveRecord API docs.

If you want to return a Book object, use Single Table Inheritance.

Later,
Vish

No that is not want I meant - that will return a Resource not the
polymorphic object. As an example take this table:

resources

id user_id resource_id resource_type
1 1 1 Library
2 1 1 School
3 1 1 Books
4 1 2 Books

user.resources.find(1) will return a Resource

But I want the user’s Books with id = 1

That is what I am asking about. Reading my first post might be helpful.

If I am understanding you correctly, you already know the id of the
resource you want and you know the type of resource. For instance try
this:

script/console

eval(“Book”).find(1)

That should return the book object.

eval(“School”).find(1)

should return a School object with the id of 1

I would refactor your method like this:

def find_mod(id, type)
eval(type).find(id)
end

That is beautiful. Thanks.

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

Kim wrote:

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

I haven’t used polymorphic associations yet, so forgive me if this is
totally offbase, but wouldn’t a dynamic finder work in this case?

Find Book with ID 7

book = user.find_by_resource_id_and_resource_type(7, “Book”)

(Of course you could wrap this in a User method that took the ID and
type as parameters.)

I’m about to start using polymorphic associations myself, so if I’m
wrong I’d appreciate learning how you ended up solving the problem.

Thanks
Jeff

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs