user532339
user532339

Reputation:

undefined local variable or method `user'

I am trying to do the following:

index.html.erb

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>

<script>
    $(function () {
        var chart;
        $(document).ready(function () {
            chart = new Highcharts.Chart({
                chart:{
                    renderTo:'container',
                    type:'column'
                },
                title:{
                    text:'<%= Date.today.strftime("%B")%> Overtime'
                },
                xAxis:{
                    categories:[

                        '<%= Date.today.strftime("%B")%>'

                    ]
                }, 
                yAxis:{
                    min:0,
                    title:{
                        text:'Hours'
                    }
                },
                legend:{
                    layout:'vertical',
                    backgroundColor:'#FFFFFF',
                    align:'left',
                    verticalAlign:'top',
                    x:100,
                    y:70,
                    floating:true,
                    shadow:true
                },
                tooltip:{
                    formatter:function () {
                        return '' +
                                'Hours' + ': ' + this.y;
                    },

                    credits:{
                        text:'migroup-.co.uk',
                        hreft:'http://wwww.migroup.co.uk'
                    }
                },
                plotOptions:{
                    column:{
                        pointPadding:0.4,
                        borderWidth:0
                    }
                },


                series:[
                    <% @user.hospital_bookings.each do |hospital_booking| %>

                    {
                        name:'<%= hospital_booking.try(:name)%>',
                        data:[<%= hospital_booking.sum(&:overtime)%>


                        ]
                        // color: '#8F8689',
                    },
                    <% end %>
                    {
                        name:'Total',
                        data: [<%= @hospital_bookings.to_a.sum(&:overtime)%>],
                        color:'#FE9D00'
                    }
                ]
            });
        });

    });


</script>

controller.rb

def index

@user = User.all
@hospital_bookings = HospitalBooking.scope

end end

What I am trying to do is for each user, iterate over their bookings and sum their overtime.

Models

User.rb 
has_many :hospital_bookings 

Hospital_booking
belongs_to :user

What this will effectively do is get the user and sum their overtime hours. But for some reason this is not working. What I have at the moment is highcharts looking like the forward:

Imgur. Each bar represents a seperate hospital booking and their overtime. However I'd like to just total that Users overtime. So effectively it has one bar per user.

Upvotes: 1

Views: 1241

Answers (1)

rorra
rorra

Reputation: 9693

Got the problem, you are doing:

@user = User.all
@user.hospital_bookings.each do |hospital_booking|
  ...
end

But the problem is that when you do User.all, you are getting an array of Users, not a single user, so if you do:

@users = User.all
@users.each do |user|
  user.hospital_bookings.each do |hospital_booking|
  end
end

that will work, however you may want to show the bookings of a single user, probably you need to do something like

@user = User.find(params[:id])
@user.hospital_bookings.each do |hospital_booking|
  ...
end

BTW, I'm on the health industry as well, feel free to ping me at anytime :)

Upvotes: 1

Related Questions