模型屬性在視圖中正確打印,但在數據庫中保存為“nil”EDITED

[英]Model attribute printing properly in view, but saved in the database as “nil” EDITED


I'm trying to write an app that calculates sick/vacation days and how much an employee has available in either category. Here's my trouble:

我正在嘗試編寫一個應用程序,用於計算病假/休假日以及員工在任何一個類別中可用的數量。這是我的麻煩:

In my view, the duration equation works and shows the right numbers, but I've put the math in the view, which I know is bad. But when I try to use the duration equation in my employee class (so I can move the math out of the view) it doesn't work, and I think that it's because duration is saving as 'nil' for some reason. I don't know why it's doing that, as everything else has been saving in the database with whatever information I input into the form.

在我看來,持續時間方程是有效的,並顯示正確的數字,但我把數學放在視圖中,我知道這是不好的。但是當我嘗試在我的員工類中使用持續時間等式時(因此我可以將數學移出視圖)它不起作用,我認為這是因為持續時間由於某種原因保存為'nil'。我不知道它為什么這樣做,因為其他所有東西都保存在數據庫中,無論我輸入到表單中的任何信息。

Maybe it's because duration isn't inputted manually in the form, but rather reacts to the date-range?

也許是因為持續時間沒有在表格中手動輸入,而是對日期范圍做出反應?

Here's where I want to call duration in the employee model to get the math out of the 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 the model where duration is defined:

這是定義持續時間的模型:

class Furlough < ActiveRecord::Base

  attr_accessible :duration # and other stuff
  belongs_to :employee
  validates_presence_of :duration # and 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 is that in the console this is what I see:

讓我失望的是,在控制台中,這就是我所看到的:

1.9.3-p0 :008 > ryan = Furlough.find(18)
  Furlough Load (0.3ms)  SELECT "furloughs".* FROM "furloughs" WHERE "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, but then I get this:

看,它沒有,但后來我明白了:

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

I'm at a loss.

我很茫然。

3 个解决方案

#1


3  

You are supposed to use instance of the class, not the class itself, thats why you are getting all those errors.

你應該使用類的實例,而不是類本身,這就是為什么你得到所有這些錯誤。

def sick_days_used
   Furlough.duration if Furlough.description == "Sick"
end

should be :

應該 :

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

or

要么

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

if your are defining it in model

如果你在模型中定義它

#2


0  

The attributes are attributes of a Furlough instance, not the Furlough class itself.

屬性是Furlough實例的屬性,而不是Furlough類本身。

#3


0  

If you are going to use the methods as class methods then you need to add 'self' to the method definition:

如果要將方法用作類方法,則需要在方法定義中添加“self”:

def self.duration
  ...
end

Then you can call Furlough.duration.

然后你可以打電話給Furlough.duration。

The other way around (def duration) you are defining an instance method, which can only be called on an instance (an specifiic Furlogh instance).

反過來(def duration)你定義了一個實例方法,它只能在一個實例(一個特定的Furlogh實例)上調用。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/05/09/191f71c7bc99f03aaac7b37f126183a8.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com