简体   繁体   中英

Ruby Rails Loop in Controller

I have a controller that I want to use to generate reports from, my first report works fine it's simply a sorted output of my CSEmployee table. However my second report isn't giving me what I would expect. Each CS_Employee can have multiple time_entries in the TimeEntry table associated with it. I have indexed the table and created a has_many\\belongs_to relationship and all seems to work just fine in my other views. My second report needs to show all associated time_entries for each CS_Employee for a given time period. Here is what I have so far:

def cs_employees
  @cs_employees = CsEmployee.all
  @cs_employee = @cs_employees.order(:cs_name)
  respond_to do |format|
    format.html
    format.csv { send_data @cs_employee.to_csv }
    format.xlsx #{ send_data @cs_employee.to_csv(col_sep: "\t") }
  end

def schedules
  @start_date = params[:start_date] || 2.weeks.ago
  @end_date = params[:end_date] || Date.today
  @cs_employee = CSEmployee.all
  @cs_employees.each do |cs_employee|
    @cs_employee = cs_employee.find(params[:cs_employee_id])
    @time_entries = @cs_employee.time_entries.find(params[:id])
    @time_entries.each do |time_entry|
      tschedules = time_entry.where(:date => (params[:start_date]).to_date..(params[:end_date]).to_date)
      @schedules += tschedules if tschedules
    end
  end
end

When I try to Debug for @schedules there is nothing in the variable. Any help would be appreciated.

You do not seem to be populating your @cs_employees variable when you call @cs_employees.each

Also, there is way too much business logic in your controller. Move it to your model or a concern.

def schedules
  @start_date = params[:start_date] || 2.weeks.ago
  @end_date = params[:end_date] || Date.today
  @cs_employees = CsEmployee.all
  @cs_employees.each do |cs_employee|
    ....
  end
end

So, after getting responses to this question and trying some of the suggestions, it turns out my loops were a mess. I ended up moving the logic to the model where, as it was pointed out to me, it belonged. The resulting code is way more simple. Here is what it looks like now.

This is what I have in the model, the joins allow me to access all the fields I need for all my reports, not just the schedule report.

  def self.report(start_date, end_date)
    Costcenter.joins(:time_entries)
    CsEmployee.joins(:time_entries)
    Maintenancecode.joins(:time_entries)
    Unit.joins(:time_entries)
    Workcode.joins(:time_entries)
    TimeEntry.where( :date => start_date..end_date )
  end 

Here is he code I now have in my controller:

  def schedules
    @start_date = params[:start_date] || 2.weeks.ago.to_date
    @end_date = params[:end_date] || Date.today
    @schedule_report = TimeEntry.report(@start_date, @end_date)
  end

Thanks for all of the help and getting me lined out as to where my code belonged in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM