ruby on rails - Model attribute printing properly in view, but saved in the database as "nil" EDITED -


i'm trying write app calculates sick/vacation days , how employee has available in either category. here's trouble:

in view, duration equation works , shows right numbers, i've put math in view, know bad. when try use duration equation in employee class (so can move math out of view) doesn't work, , think it's because duration saving 'nil' reason. don't know why it's doing that, else has been saving in database whatever information input form.

maybe it's because duration isn't inputted manually in form, rather reacts date-range?

here's want call duration in employee model math out of view:

  def remaining_vacation_days     vacation_days - @furlough.duration if @furlough.description == "vacation"   end    def remaining_sick_days      sick_days - @furlough.duration if @furlough.description == "sick"    end 

here's model duration defined:

class furlough < activerecord::base    attr_accessible :duration # , other stuff   belongs_to :employee   validates_presence_of :duration # , other stuff    def duration     only_weekdays(date_from..date_to) - psb_holidays   end    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  end 

what's tripping me out in console see:

1.9.3-p0 :008 > ryan = furlough.find(18)   furlough load (0.3ms)  select "furloughs".* "furloughs" "furloughs"."id" = ? limit 1  [["id", 18]]  => #<furlough id: 18, duration: nil, date_from: "2013-12-20", note: "christmas vacation!", created_at: "2013-05-08 14:33:03", updated_at: "2013-05-08 14:34:07", employee_id: 16, description: "vacation", date_to: "2013-12-29">  

see, it's nil, this:

1.9.3-p0 :009 > ryan.duration  => 5  

i'm @ loss.

you supposed use instance of class, not class itself, thats why getting errors.

def sick_days_used    furlough.duration if furlough.description == "sick" end 

should :

def sick_days_used    @furlough.duration if @furlough.description == "sick" end 

or

def sick_days_used    self.duration if self.description == "sick" end 

if defining in model


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -