ruby-on-rails - Rails 3 中相关表和模型的正确命名约定是什么

标签 ruby-on-rails ruby ruby-on-rails-3 activerecord

我的应用程序中有两种用户类型:InvestorsAdvisors,它们在数据库中都有一个单独的模型和表(Investor 模型、Advisor 模型、Investors 表和顾问表)。

我需要创建一个表来保存顾问的个人资料数据和一个不同的表来保存投资者的个人资料(他们在个人资料中有完全不同的字段,所以我不能合并到一个表中)。我应该给它起什么名字?如果我将其称为 investor_profile - 那么每次我想从中获取数据时,我都必须将其命名为 investor->investor_profile->field_name 但输入两次 investor 似乎很糟糕。

有什么合适的名字建议吗?还是让它成为 investor->profile->field_name 的技巧?

谢谢。

最佳答案

您可以为 investor_profilesadvisor_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/

相关文章:

ruby-on-rails - ruby on rails devise gem 数据库在哪里

ruby-on-rails - 带有 rspec 的 rails json.jbuilder api -> ActionView::MissingTemplate

ruby - "g"在 Ruby 的 "gsub"和 Vim 的替换命令中代表什么?

ruby-on-rails-3 - 使用Devise注销用户也要注销AdminUser

ruby-on-rails - 在 Rails 中执行类似操作的正确方法

mysql - Rails 会创建任何连接到 mysql 的连接池吗?它是单线程设计吗?

ruby-on-rails - Rails 范围检查是否存在关联

ruby - 如何使用 Ruby on Rails Mustache 从 ActiveModel 在 HTML 中预览

Ruby Koans about_methods 第 123 行对象循环

ruby-on-rails - rails 5 - 无法生成 rspec :install