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

匿名 (未验证) 提交于 2019-12-03 02:48:02

问题:

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.

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!