你可以存儲investor_profiles
和advisor_profiles
不同的表有不同的模型InvestorProfile, AdvisorProfile
(兩者可能從基Profile
類繼承,假設他們有至少重疊一點點)。
但在你的模型關聯,使用:class_name
選項隱藏_profiles
:
class Investor < ActiveRecord::Base
has_one :profile, :class_name => "InvestorProfile"
end
class Advisor < ActiveRecord::Base
has_one :profile, :class_name => "AdvisorProfile"
end
# And since the profiles probably overlap in some way
# a profile base class which the other 2 profile classes extend
class Profile < ActiveRecord::Base
# base options like name, address, etc...
end
class InvestorProfile < Profile
# Investor-specific stuff
end
class AdvisorProfile < Profile
# Advisor-specific stuff
end
在實踐中,然後,你可以稱其爲self.profile
:
# Use it as
adv = Advisor.new
puts adv.profile.inspect
見ActiveRecord::Associations
documentation的說明的選項。
+1給予從配置文件繼承的可能性... – Nobita 2012-03-31 21:03:30
謝謝@邁克爾,非常好的答案,其他答案也很好,但你的信息最豐富。 – 2012-03-31 21:16:48