mardi 27 octobre 2015

Cant seem to get my string to itterate in my view

Question I have a string that will have multiple values (person_ids and how many vacation days they have) I want to be able to use this data in my view. Any help would be greatly appreciated!

Here is my MyGroupAcc model

class MyGroupAcc < ActiveRecord::Base


belongs_to :entry



def self.all_people
  where(person_id: User.all.pluck(:person_id))
end

def self.active_people
  all_people.where(is_active: 'Y')
end

def self.active_vacation_data
  active_people.select(:person_id, :accum_accrued, :taken)
end

def total_v_hours
  accum_accrued.to_d - taken.to_d
end

def total_v_days
   total_v_hours / 8
end

And here is my entry controller where my view action will be

 def my_group
   peoples_vacation_information = MyGroupAcc.active_vacation_data.all
   peoples_vacation_information.map do |person|
      @p = "person #{person.person_id} has #{person.total_v_days} vacation days"
   end
   render :my_group
 end

And here is my_group view *(haml)

%table.table.table-bordered.trace-table
  %thead
    %tr.trace-table
      %th.ts.jp{:style => 'border: solid black;'} Person ID
      %th.ts.jp{:style => 'border: solid black;'} Total Vacation Days

      %tr.trace-table
      -@p.each do |r|
        %tr.trace-table
          %td.trace-table{:style => 'border: solid black;'}= r

In my view I get this error

  udefined method `each' for "person 22076 has 3.0 vacation days":String

but in my console it get this when running this code

 peoples_vacation_information = MyGroupAcc.active_vacation_data.all
 peoples_vacation_information.map do |person|
     @p = "person #{person.person_id} has #{person.total_v_days} vacation days"
 end

Which spits out this -> which is what I would like to see in my view.

    ["person 16016 has 7.0 vacation days", "person 16256 has 0.0 vacation days", "person 16256 has 18.5 vacation days", "person 17258 has 0.0 vacation days", "person 17258 has 5.0 vacation days", "person 17371 has 0.0 vacation days", "person 17371 has 20.0 vacation days", "person 19551 has 0.0 vacation days", "person 19551 has 26.5 vacation days", "person 20850 has 0.5 vacation days", "person 20850 has 14.0 vacation days", "person 21714 has 0.5 vacation days", "person 21714 has 1.0 vacation days", "person 22076 has 0.0 vacation days", "person 22076 has 3.0 vacation days"]

Aucun commentaire:

Enregistrer un commentaire