Sunday, February 3, 2008

ActiveRecord::StatementInvalid in GreetingController#index
Mysql::Error: Unknown system variable 'NAMES': SET NAMES 'utf8'

This error occurs if you are using rails 2.0.2.

As far as I understood, in case of rails 2.0.2 when we create a new project, the default database adapter is SQLlite.
In order to use mysql, one need to explicitly tell,
rails -d mysql cookbook

Assuming that you have already created the three database tables: cookbook_development, cookbook_test, cookbook_production,
if you look at the database.yml file that is present in the config folder, you will see the the following lines
development:
adapter: mysql
encoding: utf8
database: cookbook_development
username: root
password: your_password
host: localhost

similarly for test and production.
The singular pint is the addition of the new line encoding: utf8. This is a new addition, if you were using rails version <= 2.0.1, this line is missing.
So I removed the line from development, test, production settings so that my database.yml now looks as
development:
adapter: mysql
database: cookbook_development
username: root
password: your_password
host: localhost

Now if you again run the server and try to access any function of the project from the browser the error wont occur.

No comments: