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

后端 未结 4 1391
無奈伤痛
無奈伤痛 2021-02-04 08:34

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 refacto

4条回答
  •  野的像风
    2021-02-04 09:09

    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.

提交回复
热议问题