Is there any non-deprectated option in Rails 3 where i can pass a data structure in to execute a query, rather than use t开发者_运维百科he method chaining approach ?
Consider a hash representing a set of critieria for limiting a set of records (say, Documents)..
{
:conditions => {
:account_id => 2
},
:limit => 5, # page size
:offset => 5, # (page-1) * page_size
:sort => 'id DESC'
}
This may come from a URL such as:
/documents.js?page_size=5&page=2&sidx=id&sord=DESC&filter[account_id]=2
And I want to avoid any issues of order-importance in the translation of the hash to the sequential series of calls of methods:
# which is 'right', or better ?
Document.offset(5).where(:account_id => 2).limit(5)
Document.where(:account_id => 2).limit(5).offset(5)
I'm concerned that the programmatic transformation of a set of query criteria inferred from HTTP parameters or JSON objects may be more complicated if I have to walk the hash and created chained method calls.
You can use classic find method:
Document.all :conditions => { :account_id => 2 }, :limit => 5, :offset => 5, :order => "id desc"
精彩评论