模型属性在视图中正确打印,但在数据库中保存为“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