Hi all, I have the following inside of an AR class definition: relationships=ActiveRecord::Base.connection.select_all(my_relationships_sql) relationships.each do |relationship| has_many RelatedItems, :class_name => relationship[''RelatedClass''], :foreign_key => relationship[''ForeignKey''] end This kind of works, but how can I assign the name of the relationships? my_relationships_sql retrieves a field called "RelationshipName", and what I would like to do is something like this: relationships=ActiveRecord::Base.connection.select_all(my_relationships_sql) relationships.each do |relationship| has_many :association_id => relationship[''RelationshipName''], :class_name => relationship[''RelatedClass''], :foreign_key => relationship[''ForeignKey''] end But that doesn''t work. Is this possible? Adelle.
On 4/27/05, Adelle Hartley <adelle-JNHwCBCQwwtx3z9c7Zyw2w@public.gmane.org> wrote:> Hi all, > > I have the following inside of an AR class definition: > > relationships=ActiveRecord::Base.connection.select_all(my_relationships_sql) > relationships.each do |relationship| > has_many RelatedItems, :class_name => relationship[''RelatedClass''], > :foreign_key => relationship[''ForeignKey''] > end > > This kind of works, but how can I assign the name of the relationships? > > my_relationships_sql retrieves a field called "RelationshipName", and what I > would like to do is something like this: > > relationships=ActiveRecord::Base.connection.select_all(my_relationships_sql) > relationships.each do |relationship| > has_many :association_id => relationship[''RelationshipName''], :class_name > => relationship[''RelatedClass''], :foreign_key => relationship[''ForeignKey''] > end > > > But that doesn''t work. Is this possible?I''m a little confused about what you''re doing here? Are you storing the relationships between your classes in the database? Why are you doing this? Perhaps there''s a simpler way to meet your requirements?> Adelle. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
Michael Koziarski wrote:> I''m a little confused about what you''re doing here? Are you storing > the relationships between your classes in the database? Why are you > doing this? Perhaps there''s a simpler way to meet your requirements?My schema is very dynamic. I don''t know at design time what tables will be in the database. The program adds tables as the need arises. During startup, the program enumerates the tables in the database and generates model classes on the fly. Adelle.
Earlier, I wrote:> relationships=ActiveRecord::Base.connection.select_all(my_rela > tionships_sql) > relationships.each do |relationship| > has_many RelatedItems, :class_name => > relationship[''RelatedClass''], :foreign_key => > relationship[''ForeignKey''] end > > This kind of works, but how can I assign the name of the > relationships?For anyone who is interested, I was given the solution to this on ruby-talk, once I knew the right question to ask. relationships=ActiveRecord::Base.connection.select_all(my_relationships_sql) relationships.each do |relationship| has_many relationship[''RelatinoshipName''].to_sym, :class_name => relationship[''RelatedClass''], :foreign_key => relationship[''ForeignKey''] end Adelle.
On 4/27/05, Adelle Hartley <adelle-JNHwCBCQwwtx3z9c7Zyw2w@public.gmane.org> wrote:> I have the following inside of an AR class definition: > > relationships=ActiveRecord::Base.connection.select_all(my_relationships_sql) > relationships.each do |relationship| > has_many RelatedItems, :class_name => relationship[''RelatedClass''], > :foreign_key => relationship[''ForeignKey''] > end > > This kind of works, but how can I assign the name of the relationships? > > my_relationships_sql retrieves a field called "RelationshipName", and what I > would like to do is something like this: > > relationships=ActiveRecord::Base.connection.select_all(my_relationships_sql) > relationships.each do |relationship| > has_many :association_id => relationship[''RelationshipName''], :class_name > => relationship[''RelatedClass''], :foreign_key => relationship[''ForeignKey''] > end > > But that doesn''t work. Is this possible?I''m probably not understanding, but why couldn''t you just: has_many relationship[''RelationshipName''].intern, :class_name => relationship[''RelatedClass''], :foreign_key => relationship[''ForeignKey''] -- Nathaniel <:((><