好吧,好吧,那我就只是讓大家知道我在做別人會發現它有用的希望。
我決定去與普通鋼軌型號在database.yml中配置的外部數據庫:
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql
# On Mac OS X:
# sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql
# On Mac OS X Leopard:
# sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql- config=/usr/local/mysql/bin/mysql_config
# This sets the ARCHFLAGS environment variable to your native architecture
# On Windows:
# gem install mysql
# Choose the win32 build.
# Install MySQL and put its /bin directory on your path.
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql
encoding: utf8
reconnect: false
database: myapp_development
pool: 5
username: db_user
password: db_pass
socket: /private/tmp/mysql.sock
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql
encoding: utf8
reconnect: false
database: myapp_test
pool: 5
username: db_user
password: db_pass
socket: /private/tmp/mysql.sock
production:
adapter: mysql
encoding: utf8
reconnect: false
database: my_app_production
pool: 5
username: db_user
password: db_pass
socket: /private/tmp/mysql.sock
## This is the external DB ##
external_app:
adapter: mysql
encoding: utf8
reconnect: false
database: external_db
pool: 5
username: db_user
password: db_pass
socket: /private/tmp/mysql.sock
然後用標準導軌發生器創建模型:
rails g model external_app_name/model_name --no-migrations
由於模型在現有的非Rails數據庫我不想遷移,做你想在這裏。
這將在應用程序/模型/ external_app_name/model_name.rb基本空模型,然後我修改正是如此:
class ExernalApp::ModelName < ActiveRecord::Base
establish_connection :external_app # from database.yml
set_table_name :external_app_table_name
set_primary_key :id
end
從那裏,你可以繼續發展,就好像它是一個正常的軌道模型(其中,當然如此 ; )。
這種方法也會創建任何測試和其他垃圾,你的正常生成器創建,所以你可以前進。大概你也可以用腳手架等來做到這一點,如果有一些很好的理由(不適合我,只需要模型)。
注:
這也創造了在應用程序/模型/ external_app的模塊。rb,看起來像:
module ExternalApp
def self.table_name_prefix
'external_app_'
end
end
最初我設置前綴爲'',但似乎沒有任何影響創建。
我知道這些都不是火箭科學,甚至還有很遠的計算機科學的階梯,但它是一個組織/過程的事物,我認爲這將在這部分過程中儘可能地簡化生活和「軌道」項目。
希望這會有所幫助!