我的应用程序中有两种用户类型:Investors 和 Advisors,它们在数据库中都有一个单独的模型和表(Investor 模型、Advisor 模型、Investors 表和顾问表)。
我需要创建一个表来保存顾问的个人资料数据和一个不同的表来保存投资者的个人资料(他们在个人资料中有完全不同的字段,所以我不能合并到一个表中)。我应该给它起什么名字?如果我将其称为 investor_profile - 那么每次我想从中获取数据时,我都必须将其命名为 investor->investor_profile->field_name
但输入两次 investor 似乎很糟糕。
有什么合适的名字建议吗?还是让它成为 investor->profile->field_name
的技巧?
谢谢。
最佳答案
您可以为 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有关选项的说明。
关于ruby-on-rails - Rails 3 中相关表和模型的正确命名约定是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9959615/