观看http://www.railscasts.com/中的视频时记得笔记,只有我自己才能看的懂
1
class ProjectsController < ApplicationController
def show
@project = Project.find(param[:id])
@tasks = Task.find(:all,:conditions=>['project_id=? and complete=?',@project.id,false])
@tasks = @project.task.find(:all,:conditons => ['complete = ?',false])
@tasks = @project.tasks.find_all_by_complete(false)
end
end
2
layout 查找模板的顺序为先在与Controller名字相同的模板,如果没有找到,就找application.rhtml模板。
layout可以指向一个方法。这样就可以根据条件来选择模板。
如:
layout :user_layout
....
privated
def user_layout
if true
"admin"
else
"application"
end
end
还可以为action指定特定的layout。
如:
def index
@project = Project.find(:all)
render :layout => 'admin'
#render :layout => false 不指定模板
end
3
filter_parameter_logging "password"
这样就过滤了password的值,是它不在日志中显示。保护密码
4
Task.sum(:priority)
=>SELECT sum(priority) AS sum_priority FROM tasks
Task.sum(:priority,:conditions => 'complete=0')
=>SELECT sum(priority) AS sum_priority FROM tasks WHERE (complete=0)
Task.maximum(:priority)
=>SELECT max(priority) AS max_priority FROM tasks
Task.minimum(:priority)
=>SELECT min(:priority) AS min_priority FROM tasks
p=Project.find(:first)
=>SELECT * FROM projects LIMIT 1
p.tasks.sum(:priority)
=>SELECT sum(priority) AS sum_priority FROM tasks WHERE (tasks.project_id=1)
p.tasks.sum(:priority,:conditions => 'complete=0')
=>SELECT sum(priority) AS sum_priority FROM tasks WHERE (tasks.project_id=1) AND (complete=0)
5
Task.count(:all,:conditions => ["complete=? and priority=?",false,3])
=>SELECT count(*) AS count_all FROM tasks WHERE (complete=0 and priority=3)
Task.count(:all,:conditions => ["complete=? and priority=?",false,nil])
=>SELECT count(*) AS count_all FROM tasks WHERE (complete=0 and priority=NULL)
Task.count(:all,:conditions => ["complete=? and priority IS ?",false,nil])
=>SELECT count(*) AS count_all FROM tasks WHERE (complete=0 and priority IS NULL)
Task.count(:all,:conditions => ["complete=? and priority IS ?",false,[1,3]])
=>会报错!
Task.count(:all,:conditions => ["complete=? and priority in (?)",false,[1,3]])
=>SELECT count(*) AS count_all FROM tasks WHERE (complete=0 and priority in(1,3))
Task.count(:all,:conditions => ["complete=? and priority in (?)",false,[1..3]])
=>SELECT count(*) AS count_all FROM tasks WHERE (complete=0 and priority in(1,2,3))
Task.count(:all,:conditions => {:complete =>false, :priority => 1})
=>Task count(*) AS count_all FROM tasks WHERE (tasks.'priority' = 1 AND tasks.'complete' = 1)
Task.count(:all,:conditions => {:complete =>false, :priority => nil})
=>Task count(*) AS count_all FROM tasks WHERE (tasks.'priority' IS NULL AND tasks.'complete' = 1)
Task.count(:all,:conditions => {:complete =>false, :priority => [1,3]})
=>Task count(*) AS count_all FROM tasks WHERE (tasks.'priority' IN (1,3) AND tasks.'complete' = 1)
Task.count(:all,:conditions => {:complete =>false, :priority => 1..3})
=>Task count(*) AS count_all FROM tasks WHERE (tasks.'priority' BETWEEN 1 AND 3 AND tasks.'complete' = 1)
Task.find_all_by_priority(1..3)
=>SELECT * FROM tasks WHERE (tasks.'priority' BETWEEN 1 AND 3 AND)
6 动态find查询
@task = Task.find_by_complete(false,:order = > 'created_at DESC')
7
<% flash.each do |key,msg| %>
<%= content_tag :div,msg,:id => key %>
<% end %>
8 避免n+1查询
@tasks = Task.find(:all, :include => [:project,{:comments => :user}])
9
<% pluralize project.tasks.size, 'task' %>
def self.up
add_column :projects,:tasks_count,:integer,:default => 0
Project.reset_column_information
Project.find(:all).each do |p|
p.update_attributes :tasks_count,p.tasks.length
end
end
10 group_by
def index
@tasks = Task.find(:all, :order => 'due_at,id',:limit => 50)
@task_months = @task.group_by {|t| t.due_at.beginning_of_month}
end
<% @task_months.each do |month, tasks| %>
<h2><%= month.strftime('%B') %></h2>
<% for task in tasks %>
<div class="task">
<strong><%= task.name %></strong>
due on <%= task.due_at.to_date.to_s(:long) %>
</div>
<% end %>
<% end %>
或
<% @task_months.keys.sort.each do |month| %>
<h2><%= month.strftime('%B') %></h2>
<% for task in @task_months[month] %>
<div class="task">
<strong><%= task.name %></strong>
due on <%= task.due_at.to_date.to_s(:long) %>
</div>
<% end %>
<% end %>
11 日期的格式化
在environment.rb中添加移行
Time::DATE_FORMATS[:due_time] = "due ata %B %d on %I:%M %P"
这样就可以使用了
<%= task.due_at.to_s(:due_time) %>
12 定制错误消息到error里
def due_at_string
due_at.to_s(:db)
end
def due_at_string=(due_at_str)
self.due_at = Time.parse(due_at_str)
rescue ArgumentError
@due_at_invaild = true
end
def validate
errors.add(:due_at,"不合格的日期格式") if @due_at_invaild
end
13
map.home '',:controller = 'projects', :action=>'index'
===
home_path
home_url
---------------------------------------------------------
map.task_archive 'tasks/:year/:month',:controller = 'projects', :action=>'archive'
===
task_archive_path(2007,5)
task_archive_path(:year=>2007,:month=5)