Rails 4:尝试使用arel_table.maximum.desc

时间:2015-09-11 07:02:01

标签: mysql ruby-on-rails sorting

我有一个OrderProduct模型,其中包含多对多关系。所以订单has_many产品,每个产品都有不同的价格。

现在我想用订单所含产品的最高价格对订单进行排序。

这是我的代码:

@orders = Order.group(arel_table[:id]).
                order(Product.arel_table[:price].maximum)

它与默认的ASC顺序完美配合,但是当我按照这样的DESC顺序对它进行排序时:

@orders = Order.group(arel_table[:id]).
                order(Product.arel_table[:price].maximum.desc)

我收到了这样的错误:

undefined method `desc' for #<Arel::Nodes::Max:0x007fb2ab9104a0>

如何将订单排序为DESC?

1 个答案:

答案 0 :(得分:1)

这可能有效:

Template.postTravelWizard.events({
//'click #nextStep': function(e, t) {
  'submit form': function(e) {
    e.preventDefault();
    //save something...
    var step = Session.get('stepNum');
    step = (step) < 5 ? ++step : 1;
    Session.set('stepNum', step);

    if ($('#visitor').is(':checked')) {
      console.log('visitor is checked');
      var visaselection = $('select[name="selvisatype"]').val();
      console.log('visaselection is ' + visaselection);
      // When this is working (the first two return "visaselection is undefined" and the last one returns simply
      // "visaselection is "), do something similar for ucemployee (input type=radio) and selectcampus (select element)
    }
},

请参阅:

http://www.rubydoc.info/github/rails/arel/Arel/OrderPredications#desc-instance_method