2010-11-22 3 views
0

다음과 같은 모델이 있습니다.순서가 지정된 해시 가져 오기

class Quote < ActiveRecord::Base 

    named_scope :from_affiliate_id, Proc.new { |id| 
    { :select => "COUNT(*) as count_all, date(created_at) as date_created_at", 
     :conditions => {:affiliate_id => id}, 
     :group => "date(created_at)", 

    } 
    } 
    named_scope :in_dates, Proc.new {|from,to|{ :conditions => ["date(created_at) >= ? and date(created_at) <= ?",from,to]}} 

    belongs_to :affiliate 

    def self.create_quote(value = '') 
    return if value.nil? 
    quote = self.new(:affiliate_id => value) 
    quote.save 
    end 

end 

Quote.from_affiliate_id (1)을 수행하면 다음 결과를 얻습니다.

[#<Quote >, #<Quote >, #<Quote >, #<Quote >] 
[#<Quote >, #<Quote >, #<Quote >, #<Quote >] 

대신 해시 된 해시를 얻고 싶습니다. 키로 날짜가 있어야하며 값으로 간주됩니다. 제발 도와주세요. 기대에 감사

답변

1
Quote.from_affiliate_id(1).inject({}) do |h,q| 
    h[q.created_at] = q.count_all 
    h 
end 
+0

덕분에 정말 많은 shingara을, 도움이 된 – usmanali

1

기대에 감사하는 것은 주문하지 않습니다

>> ActiveSupport::OrderedHash[4,3,2,1].to_a 
=> [[4, 3], [2, 1]] 

>> Hash[4,3,2,1].to_a 
=> [[2, 1], [4, 3]] 
관련 문제