私はSTIでマッピングされた次のクラスを持っています:
class Employee < ActiveRecord::Base
end
class StudentEmployee < Employee
# I'd like to keep university only to StudentEmployee...
end
#Just to make this example easier to understand, not using migrations
ActiveRecord::Schema.define do
create_table :employees do |table|
table.column :name, :string
table.column :salary, :integer
table.column :university, :string # Only Students
end
end
emp = Employee.create(:name=>"Joe",:salary=>20000,:university=>"UCLA")
従業員の大学フィールドの設定を禁止したいのですが、StudentEmployeesには許可します。attr_protectedを使用しようとしましたが、一括設定を防ぐだけです。
class Employee < ActiveRecord::Base
attr_protected :university
end
class StudentEmployee < Employee
attr_accessible :university
end
#This time, UCLA will not be assigned here
emp = Employee.create(:name=>"Joe",:salary=>20000,:university=>"UCLA")
emp.university = "UCLA" # but this will assign university to any student...
emp.save
puts "only Students should have univesities, but this guy has one..."+emp.university.to_s
ここでの問題は、単純な従業員のための大学をデータベースに挿入することです。もう1つの問題は、StudentEmployeeクラスでは、大学は属性であると言う方がよいと思います。従業員では、大学は目に見える属性ではないということではありません...それは自然の逆方向に進むだけです。抽象化。
ありがとう。