Reputation: 49
I tried and success Remote Conncect database(based ruby on rails).
Server with Original database based PostgreSQL and made from another ruby on rails web framework. and database has a 'hit_products' table(Model name: HitProduct)
after remote connect database, I did a Migrate.
rake db:migrate RAILS_ENV=development
and when I access Rails Console, I input this code :
HitProduct.first(3)
I expect like under result :
ubuntu:~/environment (master) $ rails c
2020-01-19 07:34:25 WARN Selenium [DEPRECATION] Selenium::WebDriver::Chrome#driver_path= is deprecated. Use Selenium::WebDriver::Chrome::Service#driver_path= instead.
Running via Spring preloader in process 8830
Loading development environment (Rails 5.2.3)
2.6.3 :001 > HitProduct.first(3)
HitProduct Load (1.0ms) SELECT "hit_products".* FROM "hit_products" ORDER BY "hit_products"."id" ASC LIMIT $1 [["LIMIT", 3]]
=> [#<HitProduct id: 1, title: "[STEAM] Merry Snowballs temporary free", created_at: "2020-01-18 03:53:22", updated_at: "2020-01-18 03:53:22">, #<HitProduct id: 2, title: "LG ultra HDTV 65UM781C3NA", created_at: "2020-01-18 03:53:22", updated_at: "2020-01-18 03:53:22">, #<HitProduct id: 3, title: "[Wemakeprice] 20 LG GRAM 17D90N-VX30K", created_at: "2020-01-18 03:53:22", updated_at: "2020-01-18 03:53:22">]
but the output is Like this :
ubuntu:~/environment (master) $ rails c
Running via Spring preloader in process 5594
Loading development environment (Rails 5.2.4.1)
2.4.0 :001 > HitProduct.first(3)
NameError: uninitialized constant HitProduct
from (irb):1
but SQL Query is worked.
ubuntu:~/environment (master) $ rails db
Password: *******
Some psql features might not work.
SSL connection (cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256)
Type "help" for help.
catch_dev=# SELECT title FROM hit_products LIMIT 3;
title
-----------------------------------------------------------
[STEAM] Merry Snowballs temporary free
LG ultra HDTV 65UM781C3NA
[Wemakeprice] 20 LG GRAM 17D90N-VX30K
(3 rows)
catch_dev=#
How Could I use ORM from another server(Use DB from remote server)?
Upvotes: 0
Views: 89
Reputation: 49
I made it!
make Model file : app/models/hit_product.rb
input under code
class HitProduct < ApplicationRecord
# establish_connection "[DATABASE ENVIRONMENT]".to_sym
establish_connection "#{Rails.env}".to_sym
# self.table_name = "[TABLE_NAME]"
self.table_name = "hit_products"
end
Upvotes: 1