简体   繁体   English

控制器中的Ruby Rails循环

[英]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. 我有一个控制器,我想用它来生成报告,我的第一个报告运行良好,它只是CSEmployee表的排序输出。 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. 每个CS_Employee可以在与之关联的TimeEntry表中具有多个time_entries。 I have indexed the table and created a has_many\\belongs_to relationship and all seems to work just fine in my other views. 我已经索引了表并创建了has_many \\ belongs_to关系,并且在其他视图中一切似乎都正常。 My second report needs to show all associated time_entries for each CS_Employee for a given time period. 我的第二个报告需要显示给定时间段内每个CS_Employee的所有关联的time_entries。 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. 当我尝试调试@schedules时,变量中没有任何内容。 Any help would be appreciated. 任何帮助,将不胜感激。

You do not seem to be populating your @cs_employees variable when you call @cs_employees.each 调用@cs_employees.each时,似乎并没有填充@cs_employees变量。

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. 感谢您提供的所有帮助,并让我了解我的代码最初的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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