Monday, 15 March 2010

ruby - Rails: Pie chart for Countries that senders of a user's messages are from -


I want to feed an interactive pie chart with data that is from the sender of the user's messages,

The output should look like this:

  "[[Country: \" US \ ", UserCount: 25}, {Country: \" UK \ ", UserCount: 15}, { Country: \ "Italy \", UserCount: 10}, {Country: \ "Poland \", UserCount: 2}, {Country: \ "Japan \", Username: 5}] "  

So I need to take current_user.messages, the sender of each of them Appropriate, the address from which countries they are, how many people reckons each country, then can I loop through which an array count of names and user selection of countries have, in the string format ... phew. Welcome help.

Association Schema as a request:

  class user & lt; ActiveRecord :: Base is_to: country has_many: message and class message & lt; ActiveRecord :: Base is_to: User End Class Country & lt; ActiveRecord :: Base attr_accessible: Name, # [...] has_many: User #table is pre-populated with a list of all countries of the world    

  user_ids = current_user.messages.collect (and: user_id) country_count_hash = {} users_ids.each do | User_id | Country = User.find (user_id). Country next country country_name [country.name] || = 0 country_count_देश [country.name] + = 1 end count_array = [] country's numerator_hash.each | Key, value | Count_array & lt; & Lt; {: Country = & gt; Key ,: usercount = & gt; Value} end count_array.to_json   

This should be done Please note that it has not been tested. But this should give you at least one idea. It would be a good idea to do this in SQL if you have a lot of users.

No comments:

Post a Comment