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

后端 未结 4 1388
無奈伤痛
無奈伤痛 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 08:58

    In tadman's answer, use gem 'mysql2' for the rails 3 since rails 3 now uses the new mysql adapter !!

    0 讨论(0)
  • 2021-02-04 09:01

    Since Rails 3.2 you can define a .railsrc file with custom command line options that will always apply to rails new

    So, if you create a file called .railsrc and put it in your home directory w/ the contents like this -d mysql it wll make mysql be your default database. You can put any of the command line options in there (including application templates which are supper awesome!)

    Run rails new --help from the command line to see all your options.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-04 09:15

    In terms of database configuration, nothing much has really changed between Rails 2 and 3 with the exception of how you load your MySQL driver. This used to be done in config/environment.rb but is now done in Gemfile:

    gem 'mysql'
    

    The default config/database.yml file is set up with SQLite, but you can easily change this over to be MySQL. A generic version looks like:

    defaults: &defaults
      adapter: mysql
      username: localdev
      password: mylocaldevpasswordwhateveritis
      host: localhost
    
    development:
      <<: *defaults
      database: project_dev
    
    test:
      <<: *defaults
      database: project_test
    

    It's the adapter declaration line that sets what driver to use.

    0 讨论(0)
提交回复
热议问题