====================================================
DATABASE RELATED
====================================================
VERY BEGINNING MySQL DB BASICS
1. Login to MySQL Monitor
mysql -u root -p
2. Create the database:
create database store_development;
create database store_test;
create database store_production;
3. Switch to this new database
use store_development
4. Create the table:
create table items (
id int not null auto_increment,
name varchar(80) not null,
description text not null,
price decimal(8,2) not null,
primary key(id)
);
5. Exit MySQL Monitor:
exit
6. Configure \config\database.yml accordingly
7. Navigate to the directory of your Rails app (via Terminal)
8. Create a model named Item and a controller named Managed this way:
ruby script/generate scaffold Item Manage
class User < ActiveRecord::Base
validates_presence_of :password, :if => :should_validate_password?
validates_presence_of :state, :on => :create
validates_presence_of :state, :if => :in_us?
attr_accessor :updating_password
def in_us?
country == 'US'
end
def should_validate_password?
updating_password || new_record?
end
end
# in controller
@user.updating_password = true
@user.save
@user.save(false) # will avoid any validation in the model
class User < ActiveRecord::Base
HUMANIZED_ATTRIBUTES = {
:email => "E-mail address"
}
def self.human_attribute_name(attr)
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
end
end