ruby-on-rails – Rails 3 Active Record关系顺序:使用hash而不
发布时间:2020-12-17 02:30:30 所属栏目:百科 来源:网络整理
导读:要在Rails 3中对关系进行排序,我们必须这样做: User.where(:activated = true).order('id ASC') 但我想这个: User.where(:activated = true).order(:id = :asc) 会更有意义,因为字段名称的转义方式应该取决于适配器(SqlLite vs Mysql vs PostgreSQL),对吧
要在Rails 3中对关系进行排序,我们必须这样做:
User.where(:activated => true).order('id ASC') 但我想这个: User.where(:activated => true).order(:id => :asc) 会更有意义,因为字段名称的转义方式应该取决于适配器(SqlLite vs Mysql vs PostgreSQL),对吧? 有类似的东西吗? 解决方法
据我所知,ActiveRecord中没有内置这种语法的选项,但是添加一个语句应该不难.我找到了lib / active_record / relation / query_methods.rb中定义的order方法.从理论上讲,你应该能够做到这样的事情:
module ActiveRecord module QueryMethods def order(*args) args.map! do |arg| if arg.is_a? Hash # Format a string out of the hash that matches the original AR style stringed_arg else arg end end super end end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |