Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have used two different databases for my rails application: MongoDB and MsSQL using mongoid and activerecord-sqlserver-adapter adaptor respectively. Everything is well but there is a problem while generate Model. The problem is "how can I generate the model that relats to MongoDB or MsSQL differently?"
For example: I want to generate People model relates to MongoID and Animal model with MsSQL. While I generate with command: rails g model Animal name:string it generates the model related to mongoid. How can I generate the model Animal with ActiveRecord that means related to MsSQL.
Please help me. Thanks

share|improve this question

2 Answers 2

Based on Using Active Record generators after Mongoid installation? I believe this should work:

rails g active_record:model Animal name:string
share|improve this answer
    
it says: Could not find generator activerecord:model. –  Ganesh Kunwar Jun 19 '13 at 4:18
    
Sorry, had a typo: 'rails g active_record:model Animal name:string' should work –  pmoreira Jun 24 '13 at 23:31

First let me just check that I've understood your question correctly:

You have 2 databases and a series of models/migrations, and you want a way to tell rails which database to use when running a migration and accessing the database using your model?

If I'm in the right area then you need to add a method to your migration which overrides the default connection() method in ActiveRecord::Migration.

def connection
  ActiveRecord::Base.establish_connection(:conn_name).connection
end

Where :conn_name is the name you gave your connection settings in config/database.yml

within your models add the line

establish_connection :conn_name

to the top of your model file and the model will now know which DB to connect to.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.