ruby on rails 4验证和虚拟属性

时间:2014-02-08 06:48:22

标签: ruby-on-rails ruby validation

我是ruby on rails的新手,我正在与一些朋友合作开发应用程序。我注意到他做了一些对我没用的验证。我可以问他,但他要到下周才能上场,他也是初学者。所以这是我的情况。我们有3个模型,医生,专业和机构:

DOCTOR

# == Schema Information
#
# Table name: doctors
#
#  id                :integer          not null, primary key
#  user_id           :integer
#  license_number    :string(255)
#  created_at        :datetime
#  updated_at        :datetime
#  specialization_id :integer
#  institution_id    :integer
#

class Doctor < ActiveRecord::Base
        # relationships
    belongs_to :user

    has_many :shared_patient_cases
    has_many :patient_cases, through: :shared_patient_cases

    has_many :medical_examinations
    has_many :patients, through: :medical_examinations

    belongs_to :specialization
    belongs_to :institution

    # validations

    validates :specialization, presence: true
    validates :institution, presence:true
    validates :license_number, presence:true,  if: Proc.new { |a| a.specialization && a.specialization.licensed == 1 }
    accepts_nested_attributes_for :medical_examinations
end

# == Schema Information
#
# Table name: institutions
#
#  id             :integer          not null, primary key
#  description    :string(255)
#  contact_number :string(255)
#  order          :integer
#  created_at     :datetime
#  updated_at     :datetime
#

class Institution < ActiveRecord::Base
  has_many :doctors
end

专业化

# == Schema Information
#
# Table name: specializations
#
#  id          :integer          not null, primary key
#  code        :string(255)
#  description :string(255)
#  order       :integer
#  created_at  :datetime
#  updated_at  :datetime
#  licensed    :integer
#

class Specialization < ActiveRecord::Base
  has_many :doctors
end

地址信息:VIEWS

https://gist.github.com/carlqt/8877706

我想知道医生模型中声明的validates :specialization, presence: truevalidates :institution, presence:true,但医生模型没有这些属性。声明为has_belongs时医生模型是否继承了它们?如果没有,它是如何工作的?

1 个答案:

答案 0 :(得分:0)

我认为他正在验证这种关系,你可以在这里查看http://edgeguides.rubyonrails.org/active_record_validations.html

连接到字段specialization_id