开发者

Rails array help - How to order an array after date

开发者 https://www.devze.com 2023-02-11 20:06 出处:网络
I am trying to sort an array after dato(date). I have tried this without luck: <%= Reklamer.where(:name => \'Orville\', :order => \'dato ASC\').all.map(&:earn) %>

I am trying to sort an array after dato(date).

I have tried this without luck:

<%= Reklamer.where(:name => 'Orville', :order => 'dato ASC').all.map(&:earn) %>

And this:

<%= Reklamer.where(:name => 'Orville').all.map(&:earn).orde开发者_开发知识库r('dato ASC') %>

And:

<%= Reklamer.where(:name => 'Orville').order('dato ASC').all.map(&:earn) %>

And:

<%= Reklamer.where(:name => 'Orville').all.order('dato ASC').map(&:earn) %>


Try this:

<%= Reklamer.where(:name => 'Orville').order('dato ASC').map(&:earn).join(', ') %>

Though in general, I would recommend not putting all that data loading code into your view. Instead, push it to your models and controllers. For example, you could do this in your controller:

def show
  @earnings = Reklamer.where(:name => 'Orville').order('dato ASC').map(&:earn)
end

Or, even better, push some of the more complicated stuff to your model:

class Reklamer
  def self.earnings
    where(:name => 'Orville').order('dato ASC').map(&:earn)
  end
end


<%= Reklamer.find(:all, :conditions => {:name => 'Orville'}, :order => 'dato ASC', :select => 'sum(earn) AS earn', :group => 'dato').map(&:earn) %>

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号