Ruby on rails ActiveRecord关联

Ruby on rails ActiveRecord关联,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我得到了以下用例: 我有三种类型的用户:广告商,出版商和管理员。每个用户都有一些共同的属性(如名称或姓氏),但也有一些独特的关联。广告商与广告和活动有关联。每个模型都有自己的另一个模型 我的问题是如何使用ActiveRecords进行建模?迁移代码是什么样子的 以下是模型类: 用户: class User < ActiveRecord :: Base require 'pbkdf2' require 'date' has_many :messages attribute

我得到了以下用例:

我有三种类型的
用户
广告商
出版商
管理员
。每个用户都有一些共同的属性(如名称或姓氏),但也有一些独特的关联。广告商与
广告和
活动有关联。每个模型都有自己的另一个模型

我的问题是如何使用
ActiveRecord
s进行建模?迁移代码是什么样子的

以下是模型类:

用户:

class User < ActiveRecord :: Base
  require 'pbkdf2'
  require 'date'

  has_many :messages

  attribute :name, :surname, :email, :password_hash, :password_salt
  attr_accessor :password, :password_confirmation, :type

  attribute :user_since, :default => lambda{ Date.today.to_s }

  [...]
end
class Publisher < User  
  has_many :websites
end
class Advertiser < User
  has_many :campaigns
  has_many :ads
end
如何修改此文件以合并上述关联


编辑:更正关联以使用复数形式。

多态关系是解决此问题的一种方法,而另一种方法是使用单表继承(STI)。每种方法都有其优缺点,您的决定可能取决于
User
的子类之间的差异。他们之间的差异越大,决策就越倾向于多态关系

使用STI方法:

# a single :users table
# one table for each of the other (non-user) models

class User < ActiveRecord::Base
  has_many :messages
end

class Publisher < User  
  has_many :websites
end

class Advertiser < User
  # if :campaign supports multiple user-types (polymorphic)
  has_many :campaigns, :as => :user
  # otherwise
  has_many :campaigns

  has_many :ads
end

class Message < ActiveRecord::Base
  belongs_to :user
end

class Campaign < ActiveRecord::Base
  # if multiple user-types will have campaigns
  belongs_to :user        # referential column should be :user_id
  # otherwise
  belongs_to :advertiser  # referential column should be :advertiser_id
end
# there should be no :users table, as User will be an abstract model class 
# instead make a table for each of all the other models

class User < ActiveRecord::Base
  self.abstract_class = true
  has_many :messages, :as => :messageable
end

class Publisher < User  
  has_many :websites
end

class Advertiser < User  
  has_many :campaigns
  has_many :ads
end

class Message < ActiveRecord::Base
  belongs_to :messageable, polymorphic: true  # referential columns should be :messageable_id and :messageable_type
end

class Campaign < ActiveRecord::Base
  # if multiple user-types will have campaigns
  belongs_to :user, polymorphic: true    # referential columns should be :user_id and :user_type
  # otherwise
  belongs_to :advertiser                 # referential column should be :advertiser_id
end
#单个:用户表
#其他(非用户)模型各有一个表
类用户:用户
#否则
有很多:运动
你有很多广告吗
结束
类消息
使用多态方法:

# a single :users table
# one table for each of the other (non-user) models

class User < ActiveRecord::Base
  has_many :messages
end

class Publisher < User  
  has_many :websites
end

class Advertiser < User
  # if :campaign supports multiple user-types (polymorphic)
  has_many :campaigns, :as => :user
  # otherwise
  has_many :campaigns

  has_many :ads
end

class Message < ActiveRecord::Base
  belongs_to :user
end

class Campaign < ActiveRecord::Base
  # if multiple user-types will have campaigns
  belongs_to :user        # referential column should be :user_id
  # otherwise
  belongs_to :advertiser  # referential column should be :advertiser_id
end
# there should be no :users table, as User will be an abstract model class 
# instead make a table for each of all the other models

class User < ActiveRecord::Base
  self.abstract_class = true
  has_many :messages, :as => :messageable
end

class Publisher < User  
  has_many :websites
end

class Advertiser < User  
  has_many :campaigns
  has_many :ads
end

class Message < ActiveRecord::Base
  belongs_to :messageable, polymorphic: true  # referential columns should be :messageable_id and :messageable_type
end

class Campaign < ActiveRecord::Base
  # if multiple user-types will have campaigns
  belongs_to :user, polymorphic: true    # referential columns should be :user_id and :user_type
  # otherwise
  belongs_to :advertiser                 # referential column should be :advertiser_id
end
#应该没有:users表,因为User将是一个抽象模型类
#取而代之的是,为所有其他模型各做一张表
类用户:messageable
结束
类发布者<用户
有很多:网站
结束
类广告客户<用户
有很多:运动
你有很多广告吗
结束
类消息
这些课程正是我发布它们的方式。你能补充一个关于如何通过STI工作的简短示例吗?我开始使用STI是因为我找到了一个似乎适合我需要的教程,但我在最初的帖子中偶然发现了这个问题。澄清点,但不是STI模型吗?@zeantsoi-是的,它们在属性方面共享同一个表,这似乎不是问题。这些关联是不同的,它们显然有很多关联,这意味着引用列被添加到子模型中。再说一次,这不是问题。@PinnyM,我同意STI适合关联性很重要的用例,我不完全相信这个特定问题仅限于关联性。相反,据我所知,OP截断了代码,只显示关联的差异——可能(也可能不)有不同的属性。@zeantsoi-同意,这就是为什么在确定这一点之前,这两个选项都应该放在表上的原因。