Jump to content

The ultimate community for Ruby on Rails developers.


How does ActiveRecord connects to the database?

activerecord database beginne

  • Please log in to reply
2 replies to this topic

#1 lpcarignan



  • Members
  • 3 posts

Posted 18 April 2014 - 11:15 PM

Hi all,


I'm a beginner with RoR and Ruby so please bear with me. I have a .NET/Java background.


Can somebody explain how ActiveRecord actually connects to the database? Is it all done behind the scene everytime I do a call to the model? If so, do I just put my connection info in database.yml? In this case, could somebody point me to a page where there would be code samples on how to configure each database connection (mysql, postgres, sqlite3, etc)


Or do I have to explicity call something like this: ActiveRecord::Base.establish_connection(dbconfig) to make sure other calls will work?


Thank you for your help,



#2 Norm



  • Members
  • 7 posts
  • LocationPrescott, AZ, USA

Posted 19 April 2014 - 05:21 PM

When you create a rails app with the command 'rails new myapp' the database.yml file that defines access to the database is defined.  It will use a default setup (I think sqlite3 is the qurrent default) but there are options to create the database.yml file for mysql and postgres and possibly others.  Edit the defined file to add password and username and change database name if needed.  rails --help should get you a list of the options.  Some databases will require loading of a driver and some will require installlation of a gem.


In all cases the system connects to the database anew on each action using the configuration options in database.yml

#3 lpcarignan



  • Members
  • 3 posts

Posted 20 April 2014 - 11:58 PM

Thanks Norm for helping me in my understanding of Rails.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users