繁体   English   中英

设置默认数据库连接 Rails

[英]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.

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