Hello There''s a polymorphic association Person -> Adult. There''s an AssociationProxy array of Adult, how to convert this proxy of Person to corresponding AssociationProxy of Adult? 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 -~----------~----~----~----~------~----~------~--~---
Normally you operate (iterate) over one of the proxies since it''s job really is to give you a collection (array) of model instances. Can you give a more concrete example of what you''re trying to accomplish? On Feb 28, 12:00 pm, Ivan Ukhov <UVS...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello > > There''s a polymorphic association Person -> Adult. There''s an > AssociationProxy array of Adult, how to convert this proxy of Person > to corresponding AssociationProxy of Adult? > > 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 -~----------~----~----~----~------~----~------~--~---
Let me give you another example, there''s a model Thing that has polymorphic association with GoodThing and BadThing. Thing also has many-to-many association with itself: Thing can have many owners and many properties among other Things. class Relation < ActiveRecord::Base belongs_to :owner, :class_name => ''Thing'', :foreign_key => :owner_id belongs_to :property, :class_name => ''Thing'', :foreign_key => :property_id end class Thing < ActiveRecord::Base belongs_to :instance, :polymorphic => true has_many :relations_to, :class_name => ''Relation'', :foreign_key => :owner_id has_many :relations_from, :class_name => ''Relation'', :foreign_key => :property_id has_many :properties, :through => :relations_to has_many :owners, :through => :relations_from end class GoodThing < ActiveRecord::Base has_one :thing, :as => :instance end class BadThing < ActiveRecord::Base has_one :thing, :as => :instance end I want to perform operation like: new_good = GoodThing.new some_thing.properties.good_things << new_good new_bad = BadThings.new some_thing.owners.bad_things << new_bad some_thing.owners.find :all, :condition => {...} or even some_[good | bad]_thing.[owners | properties] << another_thing I found out that there is a nice plugin has_finder: http://pivots.pivotallabs.com/users/nick/blog/articles/284-hasfinder-it-s-now-easier-than-ever-to-create-complex-re-usable-sql-queries so I can add to Thing class Thing has_finder :bad_things, :conditions => { :instance_type => ''BadThing'' } has_finder :good_things, :conditions => { :instance_type => ''GoodThing'' } end and write something like some_thing.owners.bad_things << new_thing The problem is that these AssociationProxies (Thing.bad_things, Thing.good_things) contain objects of class Thing, not BadThing or GoodThing and I need to do something like new_good_thing = GoodThing.new new_thing = Thing.new new_thing.instance = new_good_thing some_thing.owners << new_thing And I wanna operate directly with an AssociationProxy of BadThing or GoodThings... something like that)) i hope you can get what i mean Thanks On Feb 28, 8:40 pm, AndyV <a...-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org> wrote:> Normally you operate (iterate) over one of the proxies since it''s job > really is to give you a collection (array) of model instances. > > Can you give a more concrete example of what you''re trying to > accomplish? > > On Feb 28, 12:00 pm, Ivan Ukhov <UVS...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello > > > There''s a polymorphic association Person -> Adult. There''s an > > AssociationProxy array of Adult, how to convert this proxy of Person > > to corresponding AssociationProxy of Adult? > > > 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 -~----------~----~----~----~------~----~------~--~---
Sounds like you might have a much simpler data model if you use a polymorphic association. This will allow you to have a relationship to many other properties without regard to what class they are. The only attribute that you care about is their relationship. class Property < AR::Base # This is done for relationships back to this class has_one :property_relationship, :as=>:subproperty has_many :property_relationships has_many :good_properties, :through=>:property_relationships, :conditions=>{:subproperty_type=>GoodProperty.name} has_many :bad_properties, :through=>:property_relationships, :conditions=>{:subproperty_type=>BadProperty.name} has_many :super_properties, :through=>:property_relationships, :conditions=>{:subproperty_type=>Property.name} end class PropertyRelationship < AR::Base belongs_to :property belongs_to :subproperty, :polymorphic => true end class GoodSubproperty < AR::Base has_one :property_relationship, :as=>:subproperty end class BadSubproperty << AR::Base has_one :property_relationship, :as=>:subproperty end On Feb 28, 1:22 pm, Ivan Ukhov <UVS...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Let me give you another example, there''s a model Thing that has > polymorphic association with GoodThing and BadThing. Thing also has > many-to-many association with itself: Thing can have many owners and > many properties among other Things. > > class Relation < ActiveRecord::Base > belongs_to :owner, :class_name => ''Thing'', :foreign_key => :owner_id > belongs_to :property, :class_name => ''Thing'', :foreign_key > => :property_id > end > > class Thing < ActiveRecord::Base > belongs_to :instance, :polymorphic => true > > has_many :relations_to, :class_name => ''Relation'', :foreign_key > => :owner_id > has_many :relations_from, :class_name => ''Relation'', :foreign_key > => :property_id > > has_many :properties, :through => :relations_to > has_many :owners, :through => :relations_from > end > > class GoodThing < ActiveRecord::Base > has_one :thing, :as => :instance > end > > class BadThing < ActiveRecord::Base > has_one :thing, :as => :instance > end > > I want to perform operation like: > > new_good = GoodThing.new > some_thing.properties.good_things << new_good > > new_bad = BadThings.new > some_thing.owners.bad_things << new_bad > > some_thing.owners.find :all, :condition => {...} > > or even > > some_[good | bad]_thing.[owners | properties] << another_thing > > I found out that there is a nice plugin has_finder: > > http://pivots.pivotallabs.com/users/nick/blog/articles/284-hasfinder-... > > so I can add to Thing > > class Thing > has_finder :bad_things, :conditions => { :instance_type => > ''BadThing'' } > has_finder :good_things, :conditions => { :instance_type => > ''GoodThing'' } > end > > and write something like > > some_thing.owners.bad_things << new_thing > > The problem is that these AssociationProxies (Thing.bad_things, > Thing.good_things) contain objects of class Thing, not BadThing or > GoodThing and I need to do something like > > new_good_thing = GoodThing.new > new_thing = Thing.new > new_thing.instance = new_good_thing > > some_thing.owners << new_thing > > And I wanna operate directly with an AssociationProxy of BadThing or > GoodThings... > > something like that)) i hope you can get what i mean > > Thanks > > On Feb 28, 8:40 pm, AndyV <a...-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org> wrote: > > > Normally you operate (iterate) over one of the proxies since it''s job > > really is to give you a collection (array) of model instances. > > > Can you give a more concrete example of what you''re trying to > > accomplish? > > > On Feb 28, 12:00 pm, Ivan Ukhov <UVS...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello > > > > There''s a polymorphic association Person -> Adult. There''s an > > > AssociationProxy array of Adult, how to convert this proxy of Person > > > to corresponding AssociationProxy of Adult? > > > > 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 -~----------~----~----~----~------~----~------~--~---
i dont want to create any relationships with Property, but between two Subproperties, like some Subproperty can own many others and be owned by many others: :property -> :owner, :subproperty -> :possession On Feb 29, 7:14 pm, AndyV <a...-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org> wrote:> Sounds like you might have a much simpler data model if you use a > polymorphic association. This will allow you to have a relationship > to many other properties without regard to what class they are. The > only attribute that you care about is their relationship. > > class Property < AR::Base > # This is done for relationships back to this class > has_one :property_relationship, :as=>:subproperty > > has_many :property_relationships > > has_many :good_properties, :through=>:property_relationships, :conditions=>{:subproperty_type=>GoodProperty.name} > > has_many :bad_properties, :through=>:property_relationships, :conditions=>{:subproperty_type=>BadProperty.name} > > has_many :super_properties, :through=>:property_relationships, :conditions=>{:subproperty_type=>Property.name} > end > > class PropertyRelationship < AR::Base > belongs_to :property > belongs_to :subproperty, :polymorphic => true > end > > class GoodSubproperty < AR::Base > has_one :property_relationship, :as=>:subproperty > end > > class BadSubproperty << AR::Base > has_one :property_relationship, :as=>:subproperty > end > > On Feb 28, 1:22 pm, Ivan Ukhov <UVS...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Let me give you another example, there''s a model Thing that has > > polymorphic association with GoodThing and BadThing. Thing also has > > many-to-many association with itself: Thing can have many owners and > > many properties among other Things. > > > class Relation < ActiveRecord::Base > > belongs_to :owner, :class_name => ''Thing'', :foreign_key => :owner_id > > belongs_to :property, :class_name => ''Thing'', :foreign_key > > => :property_id > > end > > > class Thing < ActiveRecord::Base > > belongs_to :instance, :polymorphic => true > > > has_many :relations_to, :class_name => ''Relation'', :foreign_key > > => :owner_id > > has_many :relations_from, :class_name => ''Relation'', :foreign_key > > => :property_id > > > has_many :properties, :through => :relations_to > > has_many :owners, :through => :relations_from > > end > > > class GoodThing < ActiveRecord::Base > > has_one :thing, :as => :instance > > end > > > class BadThing < ActiveRecord::Base > > has_one :thing, :as => :instance > > end > > > I want to perform operation like: > > > new_good = GoodThing.new > > some_thing.properties.good_things << new_good > > > new_bad = BadThings.new > > some_thing.owners.bad_things << new_bad > > > some_thing.owners.find :all, :condition => {...} > > > or even > > > some_[good | bad]_thing.[owners | properties] << another_thing > > > I found out that there is a nice plugin has_finder: > > >http://pivots.pivotallabs.com/users/nick/blog/articles/284-hasfinder-... > > > so I can add to Thing > > > class Thing > > has_finder :bad_things, :conditions => { :instance_type => > > ''BadThing'' } > > has_finder :good_things, :conditions => { :instance_type => > > ''GoodThing'' } > > end > > > and write something like > > > some_thing.owners.bad_things << new_thing > > > The problem is that these AssociationProxies (Thing.bad_things, > > Thing.good_things) contain objects of class Thing, not BadThing or > > GoodThing and I need to do something like > > > new_good_thing = GoodThing.new > > new_thing = Thing.new > > new_thing.instance = new_good_thing > > > some_thing.owners << new_thing > > > And I wanna operate directly with an AssociationProxy of BadThing or > > GoodThings... > > > something like that)) i hope you can get what i mean > > > Thanks > > > On Feb 28, 8:40 pm, AndyV <a...-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org> wrote: > > > > Normally you operate (iterate) over one of the proxies since it''s job > > > really is to give you a collection (array) of model instances. > > > > Can you give a more concrete example of what you''re trying to > > > accomplish? > > > > On Feb 28, 12:00 pm, Ivan Ukhov <UVS...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hello > > > > > There''s a polymorphic association Person -> Adult. There''s an > > > > AssociationProxy array of Adult, how to convert this proxy of Person > > > > to corresponding AssociationProxy of Adult? > > > > > 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 -~----------~----~----~----~------~----~------~--~---
Actually what i want is to connect several models with many-to-many owner/property relationships, there should be only one model Relationship that holds all the relations, with following columns: owner_id owner_type property_id property_type created_on etc and i want to easy access to all owners/properties with specific type of some object: an_instance_of_one_model.all_owners_of_this_instance_but_from_another_model << new_instance_of_that_another_model Thanks On Feb 29, 7:14 pm, AndyV <a...-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org> wrote:> Sounds like you might have a much simpler data model if you use a > polymorphic association. This will allow you to have a relationship > to many other properties without regard to what class they are. The > only attribute that you care about is their relationship. > > class Property < AR::Base > # This is done for relationships back to this class > has_one :property_relationship, :as=>:subproperty > > has_many :property_relationships > > has_many :good_properties, :through=>:property_relationships, :conditions=>{:subproperty_type=>GoodProperty.name} > > has_many :bad_properties, :through=>:property_relationships, :conditions=>{:subproperty_type=>BadProperty.name} > > has_many :super_properties, :through=>:property_relationships, :conditions=>{:subproperty_type=>Property.name} > end > > class PropertyRelationship < AR::Base > belongs_to :property > belongs_to :subproperty, :polymorphic => true > end > > class GoodSubproperty < AR::Base > has_one :property_relationship, :as=>:subproperty > end > > class BadSubproperty << AR::Base > has_one :property_relationship, :as=>:subproperty > end > > On Feb 28, 1:22 pm, Ivan Ukhov <UVS...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Let me give you another example, there''s a model Thing that has > > polymorphic association with GoodThing and BadThing. Thing also has > > many-to-many association with itself: Thing can have many owners and > > many properties among other Things. > > > class Relation < ActiveRecord::Base > > belongs_to :owner, :class_name => ''Thing'', :foreign_key => :owner_id > > belongs_to :property, :class_name => ''Thing'', :foreign_key > > => :property_id > > end > > > class Thing < ActiveRecord::Base > > belongs_to :instance, :polymorphic => true > > > has_many :relations_to, :class_name => ''Relation'', :foreign_key > > => :owner_id > > has_many :relations_from, :class_name => ''Relation'', :foreign_key > > => :property_id > > > has_many :properties, :through => :relations_to > > has_many :owners, :through => :relations_from > > end > > > class GoodThing < ActiveRecord::Base > > has_one :thing, :as => :instance > > end > > > class BadThing < ActiveRecord::Base > > has_one :thing, :as => :instance > > end > > > I want to perform operation like: > > > new_good = GoodThing.new > > some_thing.properties.good_things << new_good > > > new_bad = BadThings.new > > some_thing.owners.bad_things << new_bad > > > some_thing.owners.find :all, :condition => {...} > > > or even > > > some_[good | bad]_thing.[owners | properties] << another_thing > > > I found out that there is a nice plugin has_finder: > > >http://pivots.pivotallabs.com/users/nick/blog/articles/284-hasfinder-... > > > so I can add to Thing > > > class Thing > > has_finder :bad_things, :conditions => { :instance_type => > > ''BadThing'' } > > has_finder :good_things, :conditions => { :instance_type => > > ''GoodThing'' } > > end > > > and write something like > > > some_thing.owners.bad_things << new_thing > > > The problem is that these AssociationProxies (Thing.bad_things, > > Thing.good_things) contain objects of class Thing, not BadThing or > > GoodThing and I need to do something like > > > new_good_thing = GoodThing.new > > new_thing = Thing.new > > new_thing.instance = new_good_thing > > > some_thing.owners << new_thing > > > And I wanna operate directly with an AssociationProxy of BadThing or > > GoodThings... > > > something like that)) i hope you can get what i mean > > > Thanks > > > On Feb 28, 8:40 pm, AndyV <a...-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org> wrote: > > > > Normally you operate (iterate) over one of the proxies since it''s job > > > really is to give you a collection (array) of model instances. > > > > Can you give a more concrete example of what you''re trying to > > > accomplish? > > > > On Feb 28, 12:00 pm, Ivan Ukhov <UVS...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hello > > > > > There''s a polymorphic association Person -> Adult. There''s an > > > > AssociationProxy array of Adult, how to convert this proxy of Person > > > > to corresponding AssociationProxy of Adult? > > > > > 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 -~----------~----~----~----~------~----~------~--~---