简体   繁体   中英

Ruby on Rails - Having issues trying to connect to a mysql2 database

Basic Information

I have a rails app A that is connected to a mysql database hosted on dotcloud.com. Below is how my database.yml file looks like,

production:
  adapter: mysql2
  host: <host>
  port: <port>
  database: <database_name>
  username: <username>
  password: <password>

Until here it works fine, I can access the records in the database from A , do queries and stuff.

Now I have another rails app B that needs to be connected to the same database as A , so i simply copy and paste the exact same details above into the database.yml file in B . However, when I tried to go into the rails console on production, I can't seem to access the database. For example if I had a User Table, it will just say NameError: uninitialized constant User .

Is this an access control issue? How should I go about debugging this problem?

Appreciate any advice.


Update

So I copy and paste the .rb and schema files from A to B . However I am getting uninitialized constant ActiveRecord (NameError) when trying to access the database in rails console. Anyone know how to ?

I ended up creating a new rails app, and setup everything again. Copy the same database.yml config then did rake db:migrate, then include the necessary model files, went into rails console and it works now.

:)

Did you run the migrations for app B? Of course, the database is already set up but App B doesn't have a copy of the schema. So, you can either run the migrations in App B or copy the schema from App A to App B.

Try to list your tables using:

ActiveRecord::Base.connection.tables

That should show if you can at least get to the database correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM