Trying to u开发者_如何学Gose the will_paginate gem in rails 3, and I want to move as much of this business into the model as possible, however it seems difficult, and I haven't seen much on it. Usually, you would do something like:
Resource.where(:foo => :bar).paginate(:page => params[:page], :per_page => 10)
Is there anyway to get that params[:page]
into the model? params
isn't accessible there. My ideal would be to somehow work this into a scope
.
The params are available in a method of a model if the method takes the params as a parameter and the params are passed to the model by the controller. However, it would be better to pass only the params[:page], because the model should not know anything about the params. See, for instance, this example.
Pagination is mostly not business logic: it's about how you present the data. Hence most code involved does not belong in a model. However, will_paginate provides convenience methods to obtain results 0-15, 15-30, ... of a model and coughing up those lists is simply an ability of an activerecord model, which is why it does belong there.
精彩评论