How to connect to a remote database through a rails application

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| %>

  • <%= item.id %>
  • <% end %>

    But this code doesnt work and it gives me error.

    Can any one help?

    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.

    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”

    I tried this way but i am getting an error something like this…

    Connection refused - connect(2)

    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 [email protected] 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| %>

  • <%= item.id %>
  • <% end %>

    But this code doesnt work and it gives me error.

    Can any one help?


    Ryan B.
    http://www.frozenplague.net

    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| %>

  • <%= item.id %>
  • <% 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:
    username:
    password:
    host: <give the IP address of your remote database. eg. 192.168.1.1 >

    Also, postgresql might not be listening on all interfaces.

    On Dec 12, 2007 3:57 PM, Ryan B. [email protected] wrote:

     database: "databasename"
    

    database.yml file.

    one.
    )
    <% end %>
    http://www.frozenplague.net

    Ryan B.
    http://www.frozenplague.net

    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…

    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.

    On 13 September 2012 14:14, keerthi priya
    [email protected] 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

    On Sep 13, 2012, at 10:26 AM, Colin L. wrote:

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

    Walter

    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…

    On 14 September 2012 05:51, abhijit m. [email protected] 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

    This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

    | Privacy Policy | Terms of Service | Remote Ruby Jobs