class Employment < ActiveRecord::Base # Associations belongs_to :employee belongs_to :employer, :class_name => 'Company' # Validations validates_presence_of :employee, :employer validates_date :duration_from, :duration_to, :allow_nil => true, :allow_blank => true # Validity scope :valid_at, lambda {|value| where("duration_from <= :date AND (duration_to IS NULL OR duration_to > :date)", :date => value) } scope :valid, lambda { valid_at(Date.today) } def self.current(date = nil) date ||= Date.today valid_at(date).last end # String def to_s "%s bei %s von %s - %s" % [employee, employer, duration_from, duration_to] end # Attachments # =========== has_many :attachments, :as => :object accepts_nested_attributes_for :attachments, :reject_if => proc { |attributes| attributes['file'].blank? } end