Hi all, is there a way in RoR to dynamically set the table to which the Model must refer to? I''d like to create a sorta "dispatcher" that chooses the table for the Model based on an HTTP/GET var. To put it in another way my Model would be fed with data coming from different tables based on this var passed from the querystring. Thanks, Lorenzo -- Posted via http://www.ruby-forum.com/.
Lorenzo wrote:> is there a way in RoR to dynamically set the table to which the Model > must refer to? I''d like to create a sorta "dispatcher" that chooses the > table for the Model based on an HTTP/GET var. To put it in another way > my Model would be fed with data coming from different tables based on > this var passed from the querystring.Hi, a small correction to my previous post: I actually need to dynamically choose the _database_ to which attach to. My scenario is, infact, having N databases with the same tables structure. Was it only a table problem I could have used, I guess, the set_table_name method. Thanks again, Lorenzo -- Posted via http://www.ruby-forum.com/.
Martin Gamsjaeger
2006-May-17 17:21 UTC
[Rails] Re: Dynamically choosing schema using one Model
Hi Lorenzo, You can reference sections like foo_development: adapter: mysql database: foo username: foo password: ### host: localhost foo_test: adapter: mysql database: foo username: foo password: ### host: localhost foo_production: adapter: mysql database: foo username: foo password: ### host: localhost from your database.yml like so: class Foo< ActiveRecord::Base establish_connection "foo_#{RAILS_ENV}" end You can create different sections for your different dbs in your database.yml and reference them using establish_connection. Then you can implement your dispatcher the way you like. Does this help? cheers Martin On 5/17/06, Guest <lbolognini@gmail.com> wrote:> Lorenzo wrote: > > is there a way in RoR to dynamically set the table to which the Model > > must refer to? I''d like to create a sorta "dispatcher" that chooses the > > table for the Model based on an HTTP/GET var. To put it in another way > > my Model would be fed with data coming from different tables based on > > this var passed from the querystring. > > Hi, > > a small correction to my previous post: I actually need to dynamically > choose the _database_ to which attach to. My scenario is, infact, having > N databases with the same tables structure. Was it only a table problem > I could have used, I guess, the set_table_name method. > > Thanks again, > Lorenzo > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >