jblanche / tutoriel_ruby_rails

Tiny Web app explaining ruby and rails bases.

This URL has Read+Write access

tutoriel_ruby_rails / app / models / student.rb
100644 31 lines (23 sloc) 0.749 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Student < ActiveRecord::Base
  belongs_to :promotion
  
  validates_presence_of :firstname
  validates_presence_of :lastname
  validates_presence_of :date_of_birth
  
  named_scope :scientifics, :conditions => {:section => "S"}
  named_scope :majors , :conditions => ['date_of_birth < ?', 18.years.ago]
  named_scope :minors , :conditions => ['date_of_birth > ?', 18.years.ago]
 
  
  def to_param
    "#{id}-#{firstname}_#{lastname}"
  end
  
  def fullname
    [firstname, lastname].join(' ')
  end
  
  def age
    age = Time.now.year - date_of_birth.year
    age-=1 if Time.now.yday <= date_of_birth.yday #retirer 1 an si la date anniversaire n'est pas passée cette année
    age
  end
  
  def major?
    age >= 18
  end
end