0

I'm very, very new at Ruby on Rails. I don't know enough to even to search for the answer. I understand if you hate me but if you could please find it in your heart to throw me the teeniest, tiniest bone.

Here is my database

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

I just want to figure out how to have "sick_days" and "sick_days_used" talk to each other, as well as "vacation_days" and "vacation_days_used". Just a simple X - Y equation so I can print on the page how many vacation and sick days are still available.

Thank you for your boundless patience for such a newbie.

4

1 回答 1

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.

于 2013-05-02T20:34:40.650 回答