How do I set MySQL as the default database in Rails 3?

Viewed 21592

I started using Rails 2 last April but stopped this June because I thought learning it when Rails 3 was released would be more practical since a lot of it was completely refactored and restructured. I used to work with Ubuntu 10.04 (with SQLite3 as the default db) but now I'm using Windows 7 and MySQL 5. I already installed the gem adapter for MySQL, but to use it I still need to tweak database.yml. Thanks.

4 Answers

You can change rails to default to MySql when you generate a new application, but you have to edit a line in your rails installation. You'll have to make the change to every version, and every time you update the rails gem.

I use Ruby-Enterprise. So here's what I do:

In file (where 1.8 is the ruby version and 3.0.4 is the rails version):

/opt/ruby-enterprise/lib/ruby/gems/1.8/gems/railties-3.0.4/lib/rails/generators/rails/app/app_generator.rb

Edit: In rails-3.1.0-rc1 the file is:

gems/railties-3.1.0.rc1/lib/rails/generators/app_base.rb

Search for this line:

class_option :database, :type => :string, :aliases => "-d", :default => "sqlite3",

Change "sqlite3" to "mysql".

class_option :database, :type => :string, :aliases => "-d", :default => "mysql",

So instead of doing:

rails new application_name -d mysql

I can just do (and the database.yml and Gemfiles are configured for the mysql2 gem):

rails new application_name

This assumes you have the correct mysql2 gem installed already. Also, I've only been doing this since Rails 3 came out. It's probably similar for previous versions. Again, every time you update Rails, you'll have to find and edit that file.

Related