Rails 数据库模型控制器脚手架基础知识

	====================================================
	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

Rails 渴望加载

@tasks = Task.find(:all, :include => [:project, {:comments => :user}])

Rails 条件验证

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

Rails 重置模型以查看在迁移中添加的列

Model#reset_column_information

Rails ActiveScaffoldTools生成命令

script/generate active_scaffold TaxRate tax_rates

Rails 字符串右功能

social_security.last[4]

Rails 应用程序助手入门

def include_javascript( js_file )
  content_for( :javascripts ){ javascript_include_tag js_file }
end
  
def include_stylesheet( css_file )
  content_for( :stylesheets ){ stylesheet_link_tag css_file }
end
  
def add_body_id( id )
  content_for( :body_id ){ id }
end

def heading( page_title )
  content_for( :heading ){ page_title }
end

def class_name
  cycle "odd", "even"
end

Rails 人性化的属性

class User < ActiveRecord::Base
  HUMANIZED_ATTRIBUTES = {
    :email => "E-mail address"
  }

  def self.human_attribute_name(attr)
    HUMANIZED_ATTRIBUTES[attr.to_sym] || super
  end

end

Rails `没有这样的文件或目录 - / tmp / mysql.sock`

#go to the shell to find out where your socket actually is
locate mysql.sock

#modify database.yml accordingly
socket: /path/to/mysql.sock

Rails Rails链接到图像而不是文本

<%= link_to image_tag("search.gif", :border=>0), :action => 'show', :id => user %>