Ruby on rails 3 在Rails中查找记录(对采取何种方法感到困惑)

Ruby on rails 3 在Rails中查找记录(对采取何种方法感到困惑),ruby-on-rails-3,Ruby On Rails 3,用户有许多任务 在任务控制器视图中,需要为用户显示任务。(一个视图用于各种状态-活动、分配、关闭等) 获取任务列表的“正确”方法是什么,比如status=active #class method on Task; called from TasksController Task.find_for_user(current_user, :active) #instance method on user; called from TasksController current_user.find_

用户有许多任务

在任务控制器视图中,需要为用户显示任务。(一个视图用于各种状态-活动、分配、关闭等)

获取任务列表的“正确”方法是什么,比如status=active

#class method on Task; called from TasksController
Task.find_for_user(current_user, :active)

#instance method on user; called from TasksController
current_user.find_tasks :active (user model instance)

#the "common" way that is used in controllers and in many examples/articles
current_user.tasks.find(:where => :status = :active)  #Note the "where" part here is pseudo-code (not tested it)

使用协会是我接受培训的方式。总是工作得很好,让Rails处理SQL而无需我提供任何额外的代码

所以
当前用户.tasks.where(:status=>:active)
,或者您可以创建一个作用域

app/models/task.rb
中:

class Task < ActiveRecord::Base
    scope :active, :conditions => { :status => :active }
    ...
end 
类任务{:状态=>:active}
...
结束
并在控制器中调用
当前用户.tasks.active

也可能想看看其中包含了很多其他的好提示