0

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

4

2 回答 2

1

我认为您想查看咨询的“患者 ID”列上的“集合选择”。

于 2013-06-20T20:51:57.147 回答
1

我真的很喜欢Formtastic,因为它“理解”您的字段,例如为关联创建选择框或自动为日期创建日期选择器:

<%= semantic_form_for @consultation do |f| %>
  <%= f.inputs do %>
    <%= f.input :date %>
    <%= f.input :hour %>
    <%= f.input :doctor %>
    <%= f.input :patient %>
  <% end %>
  <%= f.actions do %>
    <%= f.action :submit, :as => :button %>
    <%= f.action :cancel, :as => :link %>
  <% end %>
<% end %>

然而,这不是一个纯粹的 Rails 解决方案,需要额外的 Gem。我不确定这是否适合您的培训目的。

于 2013-06-20T20:54:42.460 回答