Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default database connection Rails

Tags:

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 
like image 918
Chris Avatar asked Jun 26 '12 17:06

Chris


2 Answers

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

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

(found here)

like image 56
Hugeen Avatar answered Sep 19 '22 18:09

Hugeen


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)

like image 43
Nanda Avatar answered Sep 22 '22 18:09

Nanda