ruby-on-rails - 设置默认数据库连接 Rails

标签 ruby-on-rails database activerecord mongoid

我的 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 的模型

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 中的应用程序 block :

config.generators do |g|
  g.orm :active_record
end

(找到here)

关于ruby-on-rails - 设置默认数据库连接 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11213057/

相关文章:

ruby-on-rails - Rails - 添加额外的列来建模或创建额外的关联(表)?

ruby-on-rails - 数据库中的切换属性

mysql - 将一个查询的结果提供给另一个

javascript - 使用 javascript 如何计算 DIV 可见的时间,然后将该时间长度记录到本地数据库

ruby-on-rails - Rails 3 事件记录计算 : Sum and Average at the same time

ruby-on-rails - Rails 逆多态关联

ruby-on-rails - 如何使用 OpenSSL::Cipher 加密 UTF-8 字符串中的数据?

ruby-on-rails - "No such file or directory @ rb_sysopen"使用 OpenURI

ruby-on-rails - Rails 属性别名

ruby-on-rails - ruby rails : before_save fields to lowercase