I am trying to install the Ruby GEM for MySQL. I am pretty new to Ruby.
I have RVM and Bundle开发者_开发问答r installed. Should I install the gem via Bundler or RVM?
What would be the actual command to do so correctly?
Since you have RVM installed, create a gemset for your application. This'll isolate the gems for that app only and not affect the rest of the system.
rvm gemset create appname && rvm gemset use appname
Once you've done that, install gems without sudo, either manually or through bundler, ensuring the isolation.
Once you are inside the directory of your application, and after creating your gemset as Srdjan mentioned, you have two choices. Either run:
gem install mysql
or add this line to your Gemfile:
gem 'mysql'
Once you have added that line to your Gemfile, run:
bundle install
EDIT
You have to configure your database through the database.yml
file for each one of your environments (development, staging and production). That should look like this:
development:
adapter: mysql
database: name_of_your_database
encoding: utf8
host: localhost
username: root (replace with the actual username)
password: root (replace with the actual password)
Once you have that set up, you can add migrations to add tables to your database. If this is a legacy database, you might need to generate some models from it. In order to do this you can use the legacy_database gem.
You don't have to handle the connection to the database yourself, Rails will do it for you. To access your tables and query them you can use the ActiveRecord methods.
For instance, suppose you have a table called users
and you need to find a user by id, you could do this:
user = User.find(id)
Read the documentation to get running on querying using Active Record.
To get MySQL installed and running you'll need a few things:
- The development headers installed for MySQL and Ruby
- A working
mysql_config
ormysql_config5
binary gem 'mysql'
listed in yourGemfile
Once you've got all these in place, it should come together quite cleanly using the routine:
bundle install
精彩评论