26

My rails app has its own MySql database (and requires the mysql2 gem) but also needs to connect with an external MongoDB database for one particular model (and so I've included mongoid and bson_ext in the Gemfile). Now when I try to generate a migration for a new model, it tells me that

$ rails g migration CreateLocations
       error  mongoid [not found]

When I generated the Location model it included Mongoid::Document, so Rails obviously thinks it is using the external database as my primary datastore.

databse.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

UPDATE Model that uses 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
Chris
  • 11,819
  • 19
  • 91
  • 145

3 Answers3

45

Add this to the Application block in config/application.rb:

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

(found here)

Hugeen
  • 1,270
  • 14
  • 19
31

If you don't want to change the config/application.rb you could use this while generating the model:

rails generate active_record:migration

If you change the application.rb file, to invoke a mongoid generator, say for a model 'contacts', one would use:

rails g mongoid:model contacts

(solution link)

Community
  • 1
  • 1
Nanda
  • 464
  • 4
  • 6
8

First check the below block is present in config/application.rb file in your rails application

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

If not add then, or else you can run

rails g active_record:migration
Avijit Majhi
  • 508
  • 9
  • 15