0
基本的に、プロファイル可能なProfile
モデル、Staff
モデル、Client
モデルの3つのモデルがあります。Rails 4でリンクするモデルに応じて多態性モデルの属性を検証します。
Profile
モデルがStaff
用に作成されている場合、従業員は少なくとも18年前でなければなりません。 (validates_date
ヘルパーメソッドを使用して年齢を確認します。以下を参照してください) モデルがClient
用に作成されている場合、年齢制限はありません。
これを行うには?
Profile.rb:
class Profile < ActiveRecord::Base
belongs_to :profileable, polymorphic: true
validates_date :date_of_birth, :before => 16.years.ago
end
Staff.rb:
class Staff < ActiveRecord::Base
has_one :profile, as: :profileable, :dependent => :destroy
end
Client.rb:
class Client < ActiveRecord::Base
has_one :profile, as: :profileable, :dependent => :destroy
end