2011-02-03 58 views
1

我现在有在我的控制器如下:Rails:这段代码属于哪里?

@items = Item.scoped 
@items = @items.where('price >= ?', params[:price_min]) if params[:price_min] 
@items = @items.where('price <= ?', params[:price_max]) if params[:price_max] 
@items = @items.where(:size => params[:size]) if params[:size] 
@items = @items.where(:colour => params[:colour]) if params[:colour] 
# ... 
@items = @items.paginate(:page => params[:page], :per_page => 10) 

这是此代码正确的地方,还是应该真正属于我的模型与控制器一个方法调用?即

@items = Item.apply_filters(params) 

我试图尽可能地坚持约定。

非常感谢。

回答

1

另外,如果您的项目总是会先限定,你可以说:default_scope。在你的模型:

default_scope order("item_number"), where('price >= ?', 100) 

(我不能完全肯定我得到了所有的语法正确的,但它是类似的东西。)

named_scope也可以帮助你。

+0

+1的default_scope信息。谢谢。 – gjb 2011-02-03 15:40:50