在 Rails 3.x 中使用查找哈希?
Rails 3 中是否有任何未弃用的选项,我可以在其中传递数据结构来执行查询,而不是使用方法链接方法?
考虑一个代表一组用于限制一组记录(例如,文档)的标准的散列。
{
:conditions => {
:account_id => 2
},
:limit => 5, # page size
:offset => 5, # (page-1) * page_size
:sort => 'id DESC'
}
这可能来自诸如以下的 URL:
/documents.js?page_size=5&page=2&sidx=id&sord=DESC&filter[account_id]=2
并且我想避免在将散列转换为顺序时出现任何顺序重要性问题一系列方法调用:
# which is 'right', or better ?
Document.offset(5).where(:account_id => 2).limit(5)
Document.where(:account_id => 2).limit(5).offset(5)
我担心,如果我必须遍历哈希并创建链式方法调用,则从 HTTP 参数或 JSON 对象推断出的一组查询条件的编程转换可能会更加复杂。
Is there any non-deprectated option in Rails 3 where i can pass a data structure in to execute a query, rather than use the 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用经典的查找方法:
You can use classic find method: