pepe
2009-May-31 14:17 UTC
ActiveRecord not as smart as I thought it would be? or is it me?
Hello, I have been wrestling for a couple of days trying to make my application work with 2 different databases simultaneously. Finally got it running but I am a bit disappointed at what I needed to do in order to make it work. I have a couple case scenarios: First case scenario: MySQL A: 6 tables MySQL B: 1 table Following instructions from the AWDWR book and any other source of information I''ve read I add an **ActiveRecord::Base.establish_connection()** declaration in the model for the table in MySQL B. Seems simple, but did not work. I tried adding the connection parameters inside the model and also using a symbol to reference a ''database.yml'' entry. Nothing. Finally, when I was getting ready to ask for help I had the idea that made it work. I added **self.table_name = ''my_db.my_table_name''** in every model (including the main DB, named ''A'' in this example) and everything started working. Second case scenario: MySQL: 6 tables Oracle: 1 table Same as above but in addition to adding the **self.table_name** stuff I needed to add in every table of the main (MySQL) DB an **ActiveRecord::Base.establish_connection()** declaration pointing to either ''development'', ''test'' or ''production'', depending on which DB I was using. I thought that Rails would ''know'' which DB to use. Am I wrong? Should I have added any type of setup value in ''environment.rb''/other place to avoid all this? Thanks. Pepe
Maurício Linhares
2009-May-31 15:59 UTC
Re: ActiveRecord not as smart as I thought it would be? or is it me?
Stupid question, did you do: "ActiveRecord::Base.establish_connection" or did you just call "establish_connection" in your model? As you might imagine right now, calling "ActiveRecord::Base.establish_connection" is very different than calling establish_connection in the model you want to connect to another database. - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Sun, May 31, 2009 at 11:17 AM, pepe <Pepe-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> wrote:> > Hello, > > I have been wrestling for a couple of days trying to make my > application work with 2 different databases simultaneously. Finally > got it running but I am a bit disappointed at what I needed to do in > order to make it work. I have a couple case scenarios: > > First case scenario: > MySQL A: 6 tables > MySQL B: 1 table > > Following instructions from the AWDWR book and any other source of > information I''ve read I add an > **ActiveRecord::Base.establish_connection()** declaration in the model > for the table in MySQL B. > > Seems simple, but did not work. I tried adding the connection > parameters inside the model and also using a symbol to reference a > ''database.yml'' entry. Nothing. > > Finally, when I was getting ready to ask for help I had the idea that > made it work. I added **self.table_name = ''my_db.my_table_name''** in > every model (including the main DB, named ''A'' in this example) and > everything started working. > > > Second case scenario: > MySQL: 6 tables > Oracle: 1 table > > Same as above but in addition to adding the **self.table_name** stuff > I needed to add in every table of the main (MySQL) DB an > **ActiveRecord::Base.establish_connection()** declaration pointing to > either ''development'', ''test'' or ''production'', depending on which DB I > was using. > > > I thought that Rails would ''know'' which DB to use. Am I wrong? Should > I have added any type of setup value in ''environment.rb''/other place > to avoid all this? > > Thanks. > > Pepe > > >
pepe
2009-May-31 20:45 UTC
Re: ActiveRecord not as smart as I thought it would be? or is it me?
Thanks Maurício, For that extra table I used either ActiveRecord::Base.establish_connection( :ube ) in the model, with the following in database.yml: ube: adapter: mysql database: ube user: root or ActiveRecord::Base.establish_connection( :adapter => ''mysql'', :database => ''ube'', :user => ''my_user'' ) in the model and no :ube declaration in database.yml As I understand using ''establish_connection'' by itself would connect me to the ''current'' DB, wouldn''t it? Thanks. Pepe On May 31, 11:59 am, Maurício Linhares <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Stupid question, did you do: "ActiveRecord::Base.establish_connection" > or did you just call "establish_connection" in your model? > > As you might imagine right now, calling > "ActiveRecord::Base.establish_connection" is very different than > calling establish_connection in the model you want to connect to > another database. > > - > Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > > On Sun, May 31, 2009 at 11:17 AM, pepe <P...-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> wrote: > > > Hello, > > > I have been wrestling for a couple of days trying to make my > > application work with 2 different databases simultaneously. Finally > > got it running but I am a bit disappointed at what I needed to do in > > order to make it work. I have a couple case scenarios: > > > First case scenario: > > MySQL A: 6 tables > > MySQL B: 1 table > > > Following instructions from the AWDWR book and any other source of > > information I''ve read I add an > > **ActiveRecord::Base.establish_connection()** declaration in the model > > for the table in MySQL B. > > > Seems simple, but did not work. I tried adding the connection > > parameters inside the model and also using a symbol to reference a > > ''database.yml'' entry. Nothing. > > > Finally, when I was getting ready to ask for help I had the idea that > > made it work. I added **self.table_name = ''my_db.my_table_name''** in > > every model (including the main DB, named ''A'' in this example) and > > everything started working. > > > Second case scenario: > > MySQL: 6 tables > > Oracle: 1 table > > > Same as above but in addition to adding the **self.table_name** stuff > > I needed to add in every table of the main (MySQL) DB an > > **ActiveRecord::Base.establish_connection()** declaration pointing to > > either ''development'', ''test'' or ''production'', depending on which DB I > > was using. > > > I thought that Rails would ''know'' which DB to use. Am I wrong? Should > > I have added any type of setup value in ''environment.rb''/other place > > to avoid all this? > > > Thanks. > > > Pepe
Maurício Linhares
2009-May-31 21:17 UTC
Re: ActiveRecord not as smart as I thought it would be? or is it me?
Here''s how it would look like: class YourModel < ActiveRecord::Base establish_connection :ube end - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Sun, May 31, 2009 at 5:45 PM, pepe <Pepe-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> wrote:> > Thanks Maurício, > > For that extra table I used either > ActiveRecord::Base.establish_connection( :ube ) in the model, with the > following in database.yml: > ube: > adapter: mysql > database: ube > user: root > > or > > ActiveRecord::Base.establish_connection( > :adapter => ''mysql'', > :database => ''ube'', > :user => ''my_user'' ) > in the model and no :ube declaration in database.yml > > As I understand using ''establish_connection'' by itself would connect > me to the ''current'' DB, wouldn''t it? > > Thanks. > > Pepe > > On May 31, 11:59 am, Maurício Linhares <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> Stupid question, did you do: "ActiveRecord::Base.establish_connection" >> or did you just call "establish_connection" in your model? >> >> As you might imagine right now, calling >> "ActiveRecord::Base.establish_connection" is very different than >> calling establish_connection in the model you want to connect to >> another database. >> >> - >> Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) >> >> On Sun, May 31, 2009 at 11:17 AM, pepe <P...-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> wrote: >> >> > Hello, >> >> > I have been wrestling for a couple of days trying to make my >> > application work with 2 different databases simultaneously. Finally >> > got it running but I am a bit disappointed at what I needed to do in >> > order to make it work. I have a couple case scenarios: >> >> > First case scenario: >> > MySQL A: 6 tables >> > MySQL B: 1 table >> >> > Following instructions from the AWDWR book and any other source of >> > information I''ve read I add an >> > **ActiveRecord::Base.establish_connection()** declaration in the model >> > for the table in MySQL B. >> >> > Seems simple, but did not work. I tried adding the connection >> > parameters inside the model and also using a symbol to reference a >> > ''database.yml'' entry. Nothing. >> >> > Finally, when I was getting ready to ask for help I had the idea that >> > made it work. I added **self.table_name = ''my_db.my_table_name''** in >> > every model (including the main DB, named ''A'' in this example) and >> > everything started working. >> >> > Second case scenario: >> > MySQL: 6 tables >> > Oracle: 1 table >> >> > Same as above but in addition to adding the **self.table_name** stuff >> > I needed to add in every table of the main (MySQL) DB an >> > **ActiveRecord::Base.establish_connection()** declaration pointing to >> > either ''development'', ''test'' or ''production'', depending on which DB I >> > was using. >> >> > I thought that Rails would ''know'' which DB to use. Am I wrong? Should >> > I have added any type of setup value in ''environment.rb''/other place >> > to avoid all this? >> >> > Thanks. >> >> > Pepe > > >
pepe
2009-May-31 22:03 UTC
Re: ActiveRecord not as smart as I thought it would be? or is it me?
So it was ME! :) Thanks a lot Mauricio. As usual, it was so much simpler than I thought. Works like a charm. Thanks! Pepe On May 31, 5:17 pm, Maurício Linhares <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Here''s how it would look like: > > class YourModel < ActiveRecord::Base > establish_connection :ube > end > > - > Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > > On Sun, May 31, 2009 at 5:45 PM, pepe <P...-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> wrote: > > > Thanks Maurício, > > > For that extra table I used either > > ActiveRecord::Base.establish_connection( :ube ) in the model, with the > > following in database.yml: > > ube: > > adapter: mysql > > database: ube > > user: root > > > or > > > ActiveRecord::Base.establish_connection( > > :adapter => ''mysql'', > > :database => ''ube'', > > :user => ''my_user'' ) > > in the model and no :ube declaration in database.yml > > > As I understand using ''establish_connection'' by itself would connect > > me to the ''current'' DB, wouldn''t it? > > > Thanks. > > > Pepe > > > On May 31, 11:59 am, Maurício Linhares <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > >> Stupid question, did you do: "ActiveRecord::Base.establish_connection" > >> or did you just call "establish_connection" in your model? > > >> As you might imagine right now, calling > >> "ActiveRecord::Base.establish_connection" is very different than > >> calling establish_connection in the model you want to connect to > >> another database. > > >> - > >> Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > > >> On Sun, May 31, 2009 at 11:17 AM, pepe <P...-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> wrote: > > >> > Hello, > > >> > I have been wrestling for a couple of days trying to make my > >> > application work with 2 different databases simultaneously. Finally > >> > got it running but I am a bit disappointed at what I needed to do in > >> > order to make it work. I have a couple case scenarios: > > >> > First case scenario: > >> > MySQL A: 6 tables > >> > MySQL B: 1 table > > >> > Following instructions from the AWDWR book and any other source of > >> > information I''ve read I add an > >> > **ActiveRecord::Base.establish_connection()** declaration in the model > >> > for the table in MySQL B. > > >> > Seems simple, but did not work. I tried adding the connection > >> > parameters inside the model and also using a symbol to reference a > >> > ''database.yml'' entry. Nothing. > > >> > Finally, when I was getting ready to ask for help I had the idea that > >> > made it work. I added **self.table_name = ''my_db.my_table_name''** in > >> > every model (including the main DB, named ''A'' in this example) and > >> > everything started working. > > >> > Second case scenario: > >> > MySQL: 6 tables > >> > Oracle: 1 table > > >> > Same as above but in addition to adding the **self.table_name** stuff > >> > I needed to add in every table of the main (MySQL) DB an > >> > **ActiveRecord::Base.establish_connection()** declaration pointing to > >> > either ''development'', ''test'' or ''production'', depending on which DB I > >> > was using. > > >> > I thought that Rails would ''know'' which DB to use. Am I wrong? Should > >> > I have added any type of setup value in ''environment.rb''/other place > >> > to avoid all this? > > >> > Thanks. > > >> > Pepe