jblanche / tutoriel_ruby_rails

Tiny Web app explaining ruby and rails bases.

tutoriel_ruby_rails / app / models / student.rb
0abfb7b2 » jblanche 2008-08-06 rails app 1 class Student < ActiveRecord::Base
2 belongs_to :promotion
3
4 validates_presence_of :firstname
5 validates_presence_of :lastname
6 validates_presence_of :date_of_birth
7
8 named_scope :scientifics, :conditions => {:section => "S"}
94e3c609 » jblanche 2008-08-06 better named_scope 9 named_scope :majors , :conditions => ['date_of_birth < ?', 18.years.ago]
10 named_scope :minors , :conditions => ['date_of_birth > ?', 18.years.ago]
0abfb7b2 » jblanche 2008-08-06 rails app 11
12
13 def to_param
14 "#{id}-#{firstname}_#{lastname}"
15 end
16
17 def fullname
18 [firstname, lastname].join(' ')
19 end
20
21 def age
22 age = Time.now.year - date_of_birth.year
23 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
24 age
25 end
26
27 def major?
28 age >= 18
29 end
30 end