Hi there,
I'm starting to use Rails through a little project. All the main things are between some Doctors, patients and consultations. I'm learning with a book to start my application and for now, it works well but i still need help for little twists!
For example, once a doctor is created, i can create a consultation but my consultation needs a patient and i don't understand how to render a list of patients in the creation of my consultation.
Does someone have a clue?
PS: This is my code
=> DOCTOR
require 'digest'
class Doctor < ActiveRecord::Base
attr_accessible :birthdate, :birthplace, :city, :country, :firstname, :id_card_no, :lastname, :mail, :password, :secu_no, :street, :street_number, :zip
attr_accessor :password
validates :birthdate, :birthplace, :city, :country, :firstname, :lastname, :id_card_no, :secu_no, :street, :street_number, :zip, :presence=>true
validates :id_card_no,:secu_no, :uniqueness=>true
validates :street_number, :zip, :numericality=>true
validates :password, :confirmation => true,
:length => { :within => 4..20 },
:presence => true,
:if => :password_required?
validates :mail, :uniqueness => true,
:length => { :within => 5..50 },
:format => { :with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i }
has_and_belongs_to_many :offices
has_and_belongs_to_many :specialities
has_and_belongs_to_many :secretaries
has_many :consultations
default_scope order('doctors.lastname')
before_save :encrypt_new_password
def self.authenticate(email, password)
user = find_by_email(email)
return user if user && user.authenticated?(password)
end
def authenticated?(password)
self.hashed_password == encrypt(password)
end
protected
def encrypt_new_password
return if password.blank?
self.hashed_password = encrypt(password)
end
def password_required?
hashed_password.blank? || password.present?
end
def encrypt(string)
Digest::SHA1.hexdigest(string)
end
end
=> PATIENT
class Patient < ActiveRecord::Base
attr_accessible :birthdate, :birthplace, :city, :country, :firstname, :id_card_no, :job, :lastname, :secu_no, :street, :street_number, :zip
validates :birthdate, :birthplace, :city, :country, :firstname, :lastname, :id_card_no, :secu_no, :street, :street_number, :zip, :presence=>true
validates :id_card_no,:secu_no, :uniqueness=>true
validates :street_number, :zip, :numericality=>true
has_many :consultations
default_scope order('patients.lastname')
end
=> CONSULTATION
class Consultation < ActiveRecord::Base
attr_accessible :date, :hour
validates :date, :hour, :presence=>true
belongs_to :patient
belongs_to :doctor
has_one :patient_description
has_one :consultation_file
has_and_belongs_to_many :illnesses
has_and_belongs_to_many :symptoms
end
Thanks!
Thomas