ruby - Using an equation to get back an array that isn't "nil." -
i'm trying gather data db in form of array use inject method sum of duration attribute. however, can't array of integers because duration function, not value. don't know how save value when depends on range of dates. here's i've tried:
in console:
oldman = employee.first okay, duration works here..
oldman.furlough.first.duration => 1 and here...
oldman.furlough.last.duration => 4 but when try use 'all' array...
oldman.furlough.all.duration => nomethoderror: undefined method `duration' #<array:0x007fd75651c3d8> so i've tried...
oldman.furloughs.all(:select => :duration).collect(&:id) => [nil, nil] and...
oldman.furloughs.pluck(:duration) => [nil, nil] which both bring format want, not information.
here's furloughs model duration equation:
class furlough < activerecord::base belongs_to :employee def only_weekdays(range) range.select { |d| (1..5).include?(d.wday) }.size end def psb_holidays holidays.between(date_from, date_to, :us, :observed).size end def duration (only_weekdays(date_from..date_to) - psb_holidays) end end
with dave's found work:
employee.first.furloughs.collect(&:duration) in furlough model, have new method defined follows:
def vacation_days_used self.furloughs.collect(&:duration).inject(0) { | memo, n | memo + n } end which gives me separate sums of each employee's furloughs.
Comments
Post a Comment