Forum: Ruby on Rails How to connect to a remote database through a rails application.

Posted by dhaval (Guest)
on 2007-12-11 11:41
(Received via mailing list)
Hello guys,
                 I am trying to figure out how can I connect to a
remote database and retrieve information through a rails application.
I did something like this but didnt work.
I am running rails application on sqlite3 database in my local machine
and I am trying to connect to a remote database which is a postgresql
one.
I put this code in my application controller-----

 def myprojects
    ActiveRecord::Base.establish_connection(
           :adapter  => "postgresql",
           :database => "database name",
           :host     => "192.168.10.21",
           :port     => "5432",
           :username => "username",
           :password => "password"
         )

  end
people controller code-----

before_filter :myprojects
@items = Item.find(:all)

view code.......
<% @items.each do |item| %>
  <li><%= item.id %></li>
  <% end %>


But this code doesnt work and it gives me error.

Can any one help?
Posted by harm (Guest)
on 2007-12-11 17:51
(Received via mailing list)
Why put code overwritting AR::B in your controller?
I would create a model representing the external data and use the
method establish_connection :foo. Where foo is defined in your
database.yml file.
Posted by dhaval (Guest)
on 2007-12-12 06:00
(Received via mailing list)
do you mean something like this......
model---
establish_connection :foo

database.yml----
foo:
     adapter: postgresql
     database: "database name"
     host: "host name"
     port: "post number"
     username: "username"
     password: "password"
Posted by dhaval (Guest)
on 2007-12-12 08:04
(Received via mailing list)
I tried this way but i am getting an error something like this......

Connection refused - connect(2)
Posted by Ryan Bigg (Guest)
on 2007-12-12 08:41
(Received via mailing list)
For yaml you don't need to wrap the values in strings, yaml knows what 
the
values are because they're after a :

database.yml is the way to go.

On Dec 12, 2007 3:56 PM, dhaval <dhaval.deshpandey@gmail.com> wrote:

> > database.yml----
> > > Why put code overwritting AR::B in your controller?
> > > > I am running rails application on sqlite3databasein my local machine
> > > >            :username => "username",
> > > > <% @items.each do |item| %>
> > > >   <li><%= item.id %></li>
> > > >   <% end %>
> >
> > > > But this code doesnt work and it gives me error.
> >
> > > > Can any one help?
> >
>


--
Ryan Bigg
http://www.frozenplague.net
Posted by Ryan Bigg (Guest)
on 2007-12-12 17:21
(Received via mailing list)
Also, postgresql might not be listening on all interfaces.

On Dec 12, 2007 3:57 PM, Ryan Bigg <radarlistener@gmail.com> wrote:

> >
> > >      database: "databasename"
> > > >database.yml file.
> > > > > one.
> > > > >          )
> > > > >   <% end %>
> http://www.frozenplague.net
--
Ryan Bigg
http://www.frozenplague.net
Posted by Venu Vallayil (venu)
on 2007-12-12 17:28
dhaval wrote:
> Hello guys,
>                  I am trying to figure out how can I connect to a
> remote database and retrieve information through a rails application.
> I did something like this but didnt work.
> I am running rails application on sqlite3 database in my local machine
> and I am trying to connect to a remote database which is a postgresql
> one.
> I put this code in my application controller-----
> 
>  def myprojects
>     ActiveRecord::Base.establish_connection(
>            :adapter  => "postgresql",
>            :database => "database name",
>            :host     => "192.168.10.21",
>            :port     => "5432",
>            :username => "username",
>            :password => "password"
>          )
> 
>   end
> people controller code-----
> 
> before_filter :myprojects
> @items = Item.find(:all)
> 
> view code.......
> <% @items.each do |item| %>
>   <li><%= item.id %></li>
>   <% end %>
> 
> 
> But this code doesnt work and it gives me error.
> 
> Can any one help?



Try editing your database.yml file

and put lines similar to the one given below. The sample assumes you are 
connecting to a postgress database

development:
  adapter: postgresql
  database: <name of your database>
  username: <user name to log on to your postgres db say:  postgres >
  password: <password>
  host: <give the IP address of your remote database.  eg. 192.168.1.1 >
Posted by abhijit m. (abhijit_m)
on 2012-09-13 14:53
Hello Ir, please provide chaqnges in .yml file...for Mysql...'
I have cretaed MySql datbase and now i want to connect my MySql DB on 
rails...
Posted by keerthi priya (Guest)
on 2012-09-13 15:15
(Received via mailing list)
when you are trying to create a app just add these line -d mysql you 
will
get the yml according to mysql  and change the username and password
according to your mysql password.
Posted by Colin Law (Guest)
on 2012-09-13 16:27
(Received via mailing list)
On 13 September 2012 14:14, keerthi priya 
<emailtokeerthipriya@gmail.com> wrote:
> when you are trying to create a app just add these line -d mysql you will
> get the yml according to mysql  and change the username and password
> according to your mysql password.

If you have already created the app and want to change it to mysql
then create a new app as Keerhi has suggested
rails new test_app -d mysql
then you can look at database.yml and see what is required.

Colin
Posted by Walter Davis (walterdavis)
on 2012-09-13 16:54
(Received via mailing list)
On Sep 13, 2012, at 10:26 AM, Colin Law wrote:

> Colin
Also look at the Gemfile in the new app to see the gem changes required.

Walter
Posted by abhijit m. (abhijit_m)
on 2012-09-14 06:51
keerthi priya wrote in post #1075820:
> when you are trying to create a app just add these line -d mysql you
> will
> get the yml according to mysql  and change the username and password
> according to your mysql password.

Thanks ...changing user n apssword is not issue...but it indirectly 
creating databse as appname_database...and it causing error..please 
provide Solution..
Thanks for your help...
Posted by Colin Law (Guest)
on 2012-09-14 10:12
(Received via mailing list)
On 14 September 2012 05:51, abhijit m. <lists@ruby-forum.com> wrote:
> keerthi priya wrote in post #1075820:
>> when you are trying to create a app just add these line -d mysql you
>> will
>> get the yml according to mysql  and change the username and password
>> according to your mysql password.
>
> Thanks ...changing user n apssword is not issue...but it indirectly
> creating databse as appname_database...and it causing error..please
> provide Solution..
> Thanks for your help...

I think you need to try to explain the problem again, it is not clear
what your issue is.  It might help if you post the full error message
you are getting.  Also post database.yml.

Colin
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.