mazurr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-22 17:41 UTC
Use Mysql.new() without DB data
How can I use MySQL.new() inside one of my models without specifying the specifics of the database connection? So in one of my models I have something like: m = Mysql.new("localhost","myuser","mypass","mydb") r = m.query("SELECT <some complicated SQL joining......>") How can I have that SQL statement line above use the global database connection that I already have available to ActiveRecord? I know another option is find_by_sql, like: @somemodels = Somemodel.find_by_sql("SELECT <something complicated......>") But I am not fetching objects with my SQL. According to the API, the left-hand side of that statement must be model objects. Am I right on this? Is there a way to use the global db settings while using m = Mysql.new() so I don''t have to write the user/pass/db attributes inside the model? Thanks, Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You can use ActiveRecord::Base.connection, e.g. ActiveRecord::Base.connection.select_all("some nasty sql here"); and it will return as an array of hashes. -Shawn On 3/22/07, mazurr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <mazurr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > How can I use MySQL.new() inside one of my models without specifying > the specifics of the database connection? > > So in one of my models I have something like: > m = Mysql.new("localhost","myuser","mypass","mydb") > r = m.query("SELECT <some complicated SQL joining......>") > > How can I have that SQL statement line above use the global database > connection that I already have available to ActiveRecord? > > I know another option is find_by_sql, like: > @somemodels = Somemodel.find_by_sql("SELECT <something > complicated......>") > > But I am not fetching objects with my SQL. According to the API, the > left-hand side of that statement must be model objects. > > Am I right on this? Is there a way to use the global db settings > while using m = Mysql.new() so I don''t have to write the user/pass/db > attributes inside the model? > > Thanks, > Rob > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
mazurr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-22 21:48 UTC
Re: Use Mysql.new() without DB data
Bingo. Exactly what I was needing. Thanks Shawn, Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---