我在Ruby on Rails上非常非常新。我甚至不知道要搜索答案。我明白,如果你讨厌我,但如果你能在心里发现它,那就把它扔给我最小的,最小的骨头。
ActiveRecord::Schema.define(:version => 20130502193545) do
create_table "employees", :force => true do |t|
t.string "first_name"
t.string "last_name"
t.integer "employee_id"
t.date "hire_date"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "sick_days"
t.integer "vacation_days"
t.integer "sick_days_used"
t.integer "vacation_days_used"
end
end
我只想弄清楚如何让“sick_days”和“sick_days_used”互相交谈,以及“vacation_days”和“vacation_days_used”。只是一个简单的X - Y等式,所以我可以在页面上打印有多少假期和病假仍然可用。
感谢您对这样一个新手的无限耐心。
答案 0 :(得分:1)
您可以在Employee模型上添加纯Ruby方法来计算并返回差异。
class Employee < ActiveRecord::Base
def remaining_sick_days
sick_days - sick_days_used
end
def remaining_vacation_days
vacation_days - vacation_days_used
end
end
当员工加载后,您可以使用@employee.remaining_vacation_days
获得剩余的休假日。