我想为每个用户提取数据。我person_id
从用户表中获取他们,然后使用每个人的ID来确定每个人有多少天,并在我的视图中显示出来。我不确定是否正确执行此操作,因为我先在控制器中进行迭代,然后在视图中再次进行迭代。
def how_many_days_users_have
@my_group = User.all.pluck(:person_id)
@my_group.each do |v|
@indirect_id_v = Empaccrl.where("person_id = ? and is_active = ?", '#{v]', 'Y').pluck(:a_code).first
@v_range = Empaccrl.where("person_id = ? and is_active = ?", '#{v]', 'Y').pluck(:ac).first
@v_range_taken = Empaccrl.where("person_id = ? and is_active = ?", '#{v]', 'Y').pluck(:taken).first
@total_v_hours = @v_range.to_d - @v_range_taken.to_d
@total_v_days = @total_v_hours / 8
end
然后在我看来,我用它来显示以下数据:
%tr.trace-table
-@indirect_id_v.each do |idd|
%tr.trace-table
%td.trace-table{:style => 'border: solid black;'}= idd
-@total_v_days.each do |days|
%tr.trace-table
%td.trace-table{:style => 'border: solid black;'}= days
好的,首先,将一些垃圾移到您的模型中,如下所示:
class Empaccrl < ActiveRecord::Base
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, :ac, :taken)
end
def total_v_hours
ac.to_d - taken.to_d
end
def total_v_days
total_v_hours / 8
end
end
然后,您可以使用:
peoples_vacation_information = Empaccrl.active_vacation_data.all
peoples_vacation_information.map do |person|
p "person #{person.person_id} has #{person.total_v_days} vacation days"
end
老实说,您甚至不需要所有这些,但是我不确定您为什么要做自己正在做的事情,因此我认为最好是安全的并添加一些东西。无论您不需要什么,只需忽略。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句