Steven Smith
Steven Smith

Reputation: 406

ActiveRecord Chartkick

Hi am I'm just working on ActiveRecord for Chartkick as I'm still learning

at the moment I wrote code for reservation chart

<%= column_chart Reservation.where(:user_id => current_user.id, :status => 2).group(:tool_id).sum(:total) %>

it displays fine

enter image description here

however, the red highlight is right but I want this ":tool_id" to change to more text like as "resrvation.tool_id" are the relationship to the "tool.brand"

when i wrote data table and it correct display

          <td><%= link_to reservation.tool.id, reservation.tool, target: :_blank %></td>
          <td><%= reservation.tool.brand %></td>

enter image description here

so I wonder how to tweak tell this chart as a group(:tool_id)change to tool.brand

Upvotes: 1

Views: 123

Answers (1)

Reservation.joins(:tools)
           .where(user_id: current_user.id, status: 2)
           .group('tools.brand')
           .sum('reservations.total)

This will group on the name instead of the id so that way you can get a count of all the tools with the same name :D

Upvotes: 1

Related Questions