在Ruby on Rails中按日期排序

时间:2016-04-25 13:38:05

标签: ruby-on-rails ruby

我在Ruby on Rails中迭代一组对象,如下所示:

<% subject.association.each do |horse|-> { order by "dateRode" DESC } %>

马对象有一个日期字段,名为dateRode。

我想通过这个领域订购。

我不是一个红宝石开发人员,但我尝试了很多方法来做到这一点,导致语法错误,例如

$ ruby oracle_db_connection.rb
oci8.c:654:in oci8lib_230.so: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor (OCIError)
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/ruby-oci8-2.2.2-x64-mingw32/lib/oci8/oci8.rb:142:in `initialize'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:333:in `new'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:333:in `new_connection'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:393:in `initialize'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:26:in `new'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:26:in `initialize'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/connection.rb:9:in `new'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/connection.rb:9:in `create'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:156:in `oracle_enhanced_connection'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
        from C:/Ruby23-x64/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
        from C:/Ruby23-x64/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:87:in `connection'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/model_schema.rb:230:in `table_exists?'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/primary_key.rb:97:in `get_primary_key'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/primary_key.rb:85:in `reset_primary_key'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/primary_key.rb:73:in `primary_key'
        from C:in `primary_key'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:493:in `find_nth_with_limit'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:484:in `find_nth'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:127:in `first'
        from C:in `first'
        from oracle_db_connection.rb:30:in `<main>'

如何在不编辑ActiveRecords等的情况下在我的代码中执行此操作?

4 个答案:

答案 0 :(得分:2)

您可以使用<% @subject.Association.order(dateRode: :desc).each do |horse| %> <tr> <td> <%= horse['Name'].to_s %> <%= horse['Size'] %> </td> </tr> <% end %> 方法订购集合,如下所示:

<script type="template/text" id="template">
    <td><span class="{{folder}} folder" onclick="doSomething(json)"></span>
</script>

var template = $('#template').html();
var json = {folder : 'abc', size : '123', date : '1'};
var output = Mustache.render(template, json);

$(".folder").on('click', function(){
  //make use of json object here
});

答案 1 :(得分:1)

尝试替换它:

<% @subject.Association.each do |horse| %>

<% @subject.Association.order(dateRode: :desc).each do |horse| %>

希望它会对你有所帮助。

答案 2 :(得分:1)

<% subject.association.each do |horse|-> { order by "dateRode" DESC } %>

以上是erb和sql的混合,它不会起作用。

最好的方法是在模型中创建一个由控制器调用的范围。

模型

def self.order_by_date_rode
    Model.includes(:association).order("associations.dateRode desc")
end

其中Model是模型的名称,association是您要订购的关联的名称。

控制器方法

def some_method
  @subject = Model.order_by_date_rode
end

不太推荐的方法是在视图中进行排序

<% @subject.Association.order(dateRode: :desc).each do |horse| %>

答案 3 :(得分:0)

另一种方法是你可以设置一个默认的范围来排序关联模型,如下所示

class Association < ActiveRecord::Base
  default_scope { order('dateRode DESC') }
end

所以当你<% @subject.Association时,它会自动排序