Answer the question
In order to leave comments, you need to log in
How to adjust the data to display a graph in a Ruby on Rails application?
Please help.
There is an ActiveRecord request
<%= pie_chart Uk.find_by_sql("SELECT uks.MEANING, SUM(OPLATA) FROM oplata o, uks WHERE o.UK_ID = uks.UK_ID GROUP BY uks.MEANING") %>
[{"meaning":"Имя1","sum(oplata)":448616,"id":null},{"meaning":"Имя 2","sum(oplata)":112154,"id":null},{"meaning":"Имя 3","sum(oplata)":224308,"id":null}]
{"Имя 1":448616,"Имя 2":224308,"Имя 3":112154}
class Uk < ActiveRecord::Base
has_many :oplata
end
class Oplata < ActiveRecord::Base
belongs_to :uks
end
Answer the question
In order to leave comments, you need to log in
Unfortunately, there is not enough experience, and this is probably why all attempts to run through the array with all sorts of map, each, etc. were not crowned with success. Helped apidoc and query TableName.connection.select_rows("SQL")
In the future I hope to be able to correctly rewrite this query through ActiveRecord without SQL.
You can iterate over your resulting array and make the desired hash, as an option
b = a.map{|x| {x['meaning'] => x['sum(oplata)']}}
I wrote exactly how to convert the data without thinking about what it all means. Didn't check.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question