Hi, I''m wanting to get data from a table called ea_service_provider, and have managed to do so by creating a blank file called ea_service_provider.rb and with the following contents: class EaServiceProvider < ActiveRecord::Base # model to interact with ea_service_provider table end This is so in another controller I can get the list of service providers by: @ea_service_providers = EaServiceProvider.find(:all); I''m kind of unhappy having this file around just to interact with the table. Can I use Rails to connect to an arbitrary table without needing to create blank models? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tyler MacDonald
2007-May-30 23:26 UTC
Re: Connect to db table without needing blank models?
If you find you have a lot of them, this might be worthwhile: http://drnicwilliams.com/2006/08/07/ann-dr-nics-magic-models/ Cheers, Tyler luke <lduncalfe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, > > I''m wanting to get data from a table called ea_service_provider, and > have managed to do so by creating a blank file called > ea_service_provider.rb and with the following contents: > > class EaServiceProvider < ActiveRecord::Base > # model to interact with ea_service_provider table > end > > This is so in another controller I can get the list of service > providers by: > > @ea_service_providers = EaServiceProvider.find(:all); > > I''m kind of unhappy having this file around just to interact with the > table. Can I use Rails to connect to an arbitrary table without > needing to create blank models? > > > >-- --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If you want to use ActiveRecord to access your table then you need a model. You can always create the model class on the fly and throw it away, but you need it. If you just want to do an SQL query and do not need ruby objects back, just do a query using the connection from any model class in the same database. model.connection.select_all(sql) Michael On May 30, 4:14 pm, luke <ldunca...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I''m wanting to get data from a table called ea_service_provider, and > have managed to do so by creating a blank file called > ea_service_provider.rb and with the following contents: > > class EaServiceProvider < ActiveRecord::Base > # model to interact with ea_service_provider table > end > > This is so in another controller I can get the list of service > providers by: > > @ea_service_providers = EaServiceProvider.find(:all); > > I''m kind of unhappy having this file around just to interact with the > table. Can I use Rails to connect to an arbitrary table without > needing to create blank models?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---