2

I have a table called "Weights" that holds a user's weight for a specific day. I can retrieve all relevant data as JSON with the following line of code in my controller's index action:

@weights = Weight.where(:user_id => current_user.id).to_json

The output then looks like:

[{"id":72,"user_id":71,"kilograms":94.0,"day":"2016-06-03","created_at":"2016-06-03T06:58:35.139Z","updated_at":"2016-06-03T06:58:35.139Z"},{"id":75,"user_id":71,"kilograms":34.0,"day":"2016-06-02","created_at":"2016-06-03T13:05:49.862Z","updated_at":"2016-06-03T13:05:49.862Z"},{"id":76,"user_id":71,"kilograms":56.0,"day":"2016-05-29","created_at":"2016-06-03T13:06:01.747Z","updated_at":"2016-06-03T13:06:01.747Z"}]

However, what I would need is an output that looks like:

{2016-06-03 => 94.0, 2016-06-02 => 34.0, 2016-05-29 => 56.0}

How can I achieve this? (I just started coding so I'm sorry if this is a really noob question)

Many thanks.

UPDATE My request refers to a chart I want to display using Chartkick. The documentation says:

Times can be a time, a timestamp, or a string (strings are parsed)

<%= line_chart({20.day.ago => 5, 1368174456 => 4, "2013-05-07 00:00:00 UTC" => 7}) %>

My weights_controller.rb index action now

def index

    result = {}
    Weight.where(:user_id => current_user.id).each do |weight|
      result[weight.day] = weight.kilograms
    end

  end

Thus I need my data in format described above.

2
  • You can create a new serializer if you want your approach to be optional al reusable, or you can override the as_json method in your model class if you want the customization occur always. Commented Jun 3, 2016 at 17:51
  • Nothing to excuse and it's a good beginners question. Current rails comes shipped with a gem called jsonbuilder, that you can use building views of your ressource. Some prefer ActiveModel::Serialization where you can define json outputs to your ressources, where this gem follows convention over configuration like rails itself. Did you have a look on those? Commented Jun 3, 2016 at 17:52

1 Answer 1

3

Let try this:

@result = {}
Weight.where(:user_id => current_user.id).each do |weight|
  result[weight.day] = weight.kilograms
end

Now the result will be a hash:

{'2016-06-03' => 94.0, '2016-06-02' => 34.0, '2016-05-29' => 56.0}

If you want result to be json:

@result.to_json

Update Simply use it in the view for example:

<% @result.each do |day, kg| %>
  <p>Day: <%= day %>, Kg: <%= kg %><p>
<% end %>
Sign up to request clarification or add additional context in comments.

13 Comments

Why not do a grouped query and jsonify the result?
Won't the keys look like 2016-06-03T06:58:35.139Z and not 2016-06-03?
Where exactly do I put these lines? In my index action?
@Oliver Just put it in where you want to use, in controller is just fine
@AaronBrager: Correct, the OP can easily complete that I think :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.