28

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
2
  • A workaround is comment out 'mongoid' in my Gemfile, create & run the migrations, then uncomment and rebundle. Obviously not legit. Commented Jun 26, 2012 at 17:56
  • Please post the relevant model code for the one that uses MongoDB. Commented Jun 26, 2012 at 18:21

3 Answers 3

51

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

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

(found here)

Sign up to request clarification or add additional context in comments.

Comments

33

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)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.