Ruby on rails 设置默认数据库连接轨道
我的rails应用程序有自己的MySql数据库(需要mysql2 gem),但也需要连接一个特定型号的外部MongoDB数据库(因此我在Gemfile中包含了mongoid和bson_ext)。现在,当我尝试为新模型生成迁移时,它告诉我Ruby on rails 设置默认数据库连接轨道,ruby-on-rails,database,activerecord,mongoid,Ruby On Rails,Database,Activerecord,Mongoid,我的rails应用程序有自己的MySql数据库(需要mysql2 gem),但也需要连接一个特定型号的外部MongoDB数据库(因此我在Gemfile中包含了mongoid和bson_ext)。现在,当我尝试为新模型生成迁移时,它告诉我 $ rails g migration CreateLocations error mongoid [not found] 当我生成位置模型时,它包含Mongoid::Document,因此Rails显然认为它使用外部数据库作为主要数据存储 d
$ rails g migration CreateLocations
error mongoid [not found]
当我生成位置模型时,它包含Mongoid::Document,因此Rails显然认为它使用外部数据库作为主要数据存储
database.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的模型
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
中的应用程序块:
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
解决方法是在我的Gemfile中注释掉“mongoid”,创建并运行迁移,然后取消注释并重新处理。显然不合法。请发布使用MongoDB的相关型号代码。