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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Does anyone know? I am sure that other people have needed this type of functionality. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
user.resources.find(id) Vish On 1/10/07, Kim <Kim.Griggs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Does anyone know? I am sure that other people have needed this type of > functionality. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On 1/13/07, Kim <Kim.Griggs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > 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. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Jan 16, 5:07 am, "Vishnu Gopal" <g.vis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On 1/13/07, Kim <Kim.Gri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
thoughtobject-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jan-17 15:37 UTC
Re: Polymorphic table - find one associated object
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That is beautiful. Thanks. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---