[英]Set default database connection Rails
我的 rails 應用程序有自己的 MySql 數據庫(並且需要 mysql2 gem),但還需要連接一個特定模型的外部 MongoDB 數據庫(因此我在 Gemfile 中包含了 mongoid 和 bson_ext)。 現在,當我嘗試為新模型生成遷移時,它告訴我
$ rails g migration CreateLocations
error mongoid [not found]
當我生成 Location 模型時,它包含 Mongoid::Document,因此 Rails 顯然認為它使用外部數據庫作為我的主要數據存儲。
數據庫.yml:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: associalize_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
mongoid.yml:
development:
host: pearl.mongohq.com
port: 27019
username: asfasdf
password: sadfasdf
database: app4574678
test:
host: pearl.mongohq.com
port: 27019
username: asdfadhasdfa
password: hadsadfas
database: app4574678
production:
host: pearl.mongohq.com
port: 27019
username: asdfdfsasda
password: afdasdfdasdf
database: app4574678
使用 Mongo 的UPDATE模型
class ExternalMongoModel
include Mongoid::Document
field :title
field :long_title
field :deal_type
field :merchandise_type
field :market_id
field :market_name
field :market_location, type: Array
field :featureType
field :country_code
field :subtitle
field :offer_ends_at
field :price
field :value
field :merchant_type
field :content
field :merchant
index(
[[:division_latlon, Mongo::GEO2D]], background: true
)
end
將此添加到config/application.rb
的 Application 塊:
config.generators do |g|
g.orm :active_record
end
(在這里找到)
如果您不想更改 config/application.rb 您可以在生成模型時使用它:
rails generate active_record:migration
如果您更改 application.rb 文件,以調用 mongoid 生成器,例如模型“聯系人”,則可以使用:
rails g mongoid:model contacts
( 解決方案鏈接)
首先檢查以下塊是否存在於 Rails 應用程序的 config/application.rb 文件中
config.generators do |g|
g.orm :active_record
end
如果不添加那么,否則你可以運行
rails g active_record:migration
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.