2016-11-30 13 views
0

これまでに2日間クエリを書くのに苦労しています。だから、私はこれら3つの関連のモデルの中に照会しようとしています:複数のモデルを使ってRailsクエリを実行

class Company... 
    has_many :programs 
end 

class Program... 
    belongs_to :company 
    has_many :transactions 
end 

class Transaction... 
    belongs_to :program 
end 

は出力として、私はすべての取引の量作られ、どのような日の各会社のリストが必要です。

+0

あなたは出力として必要なものをより良く説明できますか? – Fallenhero

+0

ちょっと@Fallenhero、私は質問を更新しました。今私は自分自身を明確にしたのですか、それでもそれを得られないのですか? –

+0

よく@Fallenhero、私は 'Transaction.joins(program::company)'を試して、会社IDでグループ化していました。しかし、私はこのアプローチでどこにも行けなかった。 :( –

答えて

0

あなたのクエリは次のようなものです。 http://ruby-doc.org/core-2.2.0/Time.html#method-i-strftimeを -

Company.includes(:programs => :transactions).map do |company| {:company_name => company.name, :transactions => company.programs.map{|program| program.transactions.map{|t| {:amount => t.amount, :date => t.created_at.strftime("%d-%m-%Y")}}.flatten } end

現在地から時間クラスのstrftimeのメソッドを参照することにより、日付の書式を変更することができます。

関連する問題