Pfister, Govinda
2005-Sep-06 10:05 UTC
how to dynamically establish model relationships???
Hi, I tried to find an answer to this topic in the archive, but couldn´t find anything. I do develop a rails app, which can be extended by plugins/components. Those components have their own models. Those components interact with models of the base app, too. So I have to establish relationships between models of my base app and models of components I don´t know of in advance. The components are registered with the app, when installed. So I can get the info of installed components from the database. I thought of a method, which establishes relationships dynamically at object creation via initialize() and gets the necessary infos from the database. Can this be done? Govinda _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi, is there nobody who can give me a hint in the right direction? I would be glad if there is a solution for dynamically establishing model relationsships... something like for example how you can do it with layouts (if I remember it right) layout :method_do_dynamically_establish_layout for a controller Best Regards, Govinda
Michael Schuerig
2005-Sep-07 16:49 UTC
Re: how to dynamically establish model relationships???
On Wednesday 07 September 2005 16:11, Govinda wrote:> Hi, > > is there nobody who can give me a hint in the right direction? > I would be glad if there is a solution for dynamically establishing > model relationsships... something like for example how you can do it > with layouts (if I remember it right)You understand that belongs_to, has_many, has_one, has_abtm are simply class methods? Thus you might get away with ClassToExtend.class_eval do belongs_to ... has_one ... end I haven''t tried it and don''t know the code well enough to say if it expects the set of associations to be fixed after initial definition of the class. You have to look for yourself if it works. Michael -- Michael Schuerig Thinking is trying to make up mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org for a gap in one''s education. http://www.schuerig.de/michael/ --Gilbert Ryle
Govinda, I''ve thrown something together that might help you: ---file: rel_installer.rb module ActiveRecord module RelationshipInstaller def self.included(base) base.extend(Installer) end module Installer def install_relationships(relationships) relationships.each do | class_name, assoc | class_name.to_s.constantize.class_eval assoc end end end end end ---end file - Put rel_installer.rb into your app''s lib directory. - Put "require ''rel_installer.rb''" into your environment.rb file - Put a call to ''install_relationships'' into your plugin classes. Example: class MyPlugin<<ActiveRecord::Base install_relationships "MyMainModel"=>"has_many :my_plugins", "AnotherModel"=>"has_and_belongs_to_many :my_plugins" end *When the class definition is executed* at startup, this code will open up the classes with the given names as the keys in the parameter hash and add the relationships given in the associated values. So, in the above example, the MyMainModel will have a has_many relationship to MyPlugin. - Note: In this implementation, the relationship will not be executed until the plugin class is first referenced. You could do this by simply iterating over your list of plugin class name strings and executing: ''plugin_name.constantize'' where ''plugin_name'' is a string that is the name of the model being plugged-in. - Obviously, you have choose string delimeters that take into account information embedded in the relationship parameter strings. This is the simplest implementation. You could be much smarter, but hopefully this will give you an idea of what is possible. Note: I have only minimally tested this due to time constraints (one simple check, actually), so do not put this into production without a battery of tests. And, because I live in the USA, let me add that this code is provided without warranty. Use at your own risk. Let me know if this helped you. - Scott On 9/6/05, Pfister, Govinda <Govinda.Pfister-kyawv7ubMNaakBO8gow8eQ@public.gmane.org> wrote:> > Hi, > > I tried to find an answer to this topic in the archive, but couldn´t find > anything. > > I do develop a rails app, which can be extended by plugins/components. > Those components have their own models. > > Those components interact with models of the base app, too. So I have to > establish relationships between models of my base app and models of > components I don´t know of in advance. The components are registered with > the app, when installed. So I can get the info of installed components from > the database. > > I thought of a method, which establishes relationships dynamically at > object creation via initialize() and gets the necessary infos from the > database. > > Can this be done? > > Govinda > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
oops. a few lines were lost from the file somehow: it should read: ----file: rel_installer.rb module ActiveRecord module RelationshipInstaller def self.included(base) base.extend(Installer) end module Installer def install_relationships(relationships) relationships.each do | class_name, assoc | class_name.to_s.constantize.class_eval assoc end end end end end ActiveRecord::Base.class_eval do include ActiveRecord::RelationshipInstaller end ---end file On 9/7/05, scott moody <rorpostings-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Govinda, > > I''ve thrown something together that might help you: > > ---file: rel_installer.rb > > module ActiveRecord > module RelationshipInstaller > def self.included(base) > base.extend(Installer) > end > module Installer > def install_relationships(relationships) > relationships.each do | class_name, assoc | > class_name.to_s.constantize.class_eval assoc > end > end > end > end > end > > ---end file > > - Put rel_installer.rb into your app''s lib directory. > - Put "require ''rel_installer.rb''" into your environment.rb file > > - Put a call to ''install_relationships'' into your plugin classes. Example: > > class MyPlugin<<ActiveRecord::Base > install_relationships "MyMainModel"=>"has_many :my_plugins", > "AnotherModel"=>"has_and_belongs_to_many :my_plugins" > end > > *When the class definition is executed* at startup, this code will open up > the classes with the given names as the keys in the parameter hash and add > the relationships given in the associated values. So, in the above example, > the MyMainModel will have a has_many relationship to MyPlugin. > > - Note: In this implementation, the relationship will not be executed > until the plugin class is first referenced. You could do this by simply > iterating over your list of plugin class name strings and executing: > ''plugin_name.constantize'' where ''plugin_name'' is a string that is the name > of the model being plugged-in. > > - Obviously, you have choose string delimeters that take into account > information embedded in the relationship parameter strings. > > This is the simplest implementation. You could be much smarter, but > hopefully this will give you an idea of what is possible. > > Note: I have only minimally tested this due to time constraints (one > simple check, actually), so do not put this into production without a > battery of tests. And, because I live in the USA, let me add that this code > is provided without warranty. Use at your own risk. > > Let me know if this helped you. > - Scott > > > > On 9/6/05, Pfister, Govinda <Govinda.Pfister-kyawv7ubMNaakBO8gow8eQ@public.gmane.org> wrote: > > > Hi, > > > > I tried to find an answer to this topic in the archive, but couldn´t > > find anything. > > > > I do develop a rails app, which can be extended by plugins/components. > > Those components have their own models. > > > > Those components interact with models of the base app, too. So I have to > > establish relationships between models of my base app and models of > > components I don´t know of in advance. The components are registered with > > the app, when installed. So I can get the info of installed components from > > the database. > > > > I thought of a method, which establishes relationships dynamically at > > object creation via initialize() and gets the necessary infos from the > > database. > > > > Can this be done? > > > > Govinda > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ezra Zygmuntowicz
2005-Sep-07 20:50 UTC
Re: how to dynamically establish model relationships???
Nice one Scott- -Ezra On Sep 7, 2005, at 9:59 AM, scott moody wrote:> Govinda, > > I''ve thrown something together that might help you: > > ---file: rel_installer.rb > > module ActiveRecord > module RelationshipInstaller > def self.included(base) > base.extend(Installer) > end > module Installer > def install_relationships(relationships) > relationships.each do | class_name, assoc | > class_name.to_s.constantize.class_eval assoc > end > end > end > end > end > > ---end file > > - Put rel_installer.rb into your app''s lib directory. > - Put "require ''rel_installer.rb''" into your environment.rb file > > - Put a call to ''install_relationships'' into your plugin classes. > Example: > > class MyPlugin<<ActiveRecord::Base > install_relationships "MyMainModel"=>"has_many :my_plugins", > > "AnotherModel"=>"has_and_belongs_to_many :my_plugins" > end > > *When the class definition is executed* at startup, this code will > open up the classes with the given names as the keys in the > parameter hash and add the relationships given in the associated > values. So, in the above example, the MyMainModel will have a > has_many relationship to MyPlugin. > > - Note: In this implementation, the relationship will not be > executed until the plugin class is first referenced. You could do > this by simply iterating over your list of plugin class name > strings and executing: ''plugin_name.constantize'' where > ''plugin_name'' is a string that is the name of the model being > plugged-in. > > - Obviously, you have choose string delimeters that take into > account information embedded in the relationship parameter strings. > > This is the simplest implementation. You could be much smarter, but > hopefully this will give you an idea of what is possible. > > Note: I have only minimally tested this due to time constraints > (one simple check, actually), so do not put this into production > without a battery of tests. And, because I live in the USA, let me > add that this code is provided without warranty. Use at your own risk. > > Let me know if this helped you. > - Scott > > > > On 9/6/05, Pfister, Govinda <Govinda.Pfister-kyawv7ubMNaakBO8gow8eQ@public.gmane.org> wrote: > Hi, > > > I tried to find an answer to this topic in the archive, but couldn > ´t find anything. > > > I do develop a rails app, which can be extended by plugins/ > components. Those components have their own models. > > Those components interact with models of the base app, too. So I > have to establish relationships between models of my base app and > models of components I don´t know of in advance. The components are > registered with the app, when installed. So I can get the info of > installed components from the database. > > > I thought of a method, which establishes relationships dynamically > at object creation via initialize() and gets the necessary infos > from the database. > > > Can this be done? > > > Govinda > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz Yakima Herald-Republic WebMaster 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org