【发布时间】:2010-07-01 11:15:28
【问题描述】:
我偶然发现了一个关于活动记录关联的有趣挑战:
我有一个 Account 模型,它可以附加多个不同的组织(例如公司、承包商、个人),并且每个关联(会计师、所有者、查看者等)都有不同的角色。 )。
所以我不确定将它们关联起来的最佳方式是什么。
【问题讨论】:
标签: mysql ruby-on-rails activerecord associations
我偶然发现了一个关于活动记录关联的有趣挑战:
我有一个 Account 模型,它可以附加多个不同的组织(例如公司、承包商、个人),并且每个关联(会计师、所有者、查看者等)都有不同的角色。 )。
所以我不确定将它们关联起来的最佳方式是什么。
【问题讨论】:
标签: mysql ruby-on-rails activerecord associations
我以 Elimantas 为例,纠正了一些逻辑错误。基本上,您有一个拥有 N 个组织的帐户(这是一个多态模型,并且与公司、承包商等之一有关系......)
class Account < ActiveRecord::Base
has_many :organizations
end
class Organization < ActiveRecord::Base
belongs_to :account
belongs_to :resource, :polymorphic => true
end
class Company < ActiveRecord::Base
has_many :organizations, :as => :resource
end
class Contractor < ActiveRecord::Base
has_many :organizations, :as => :resource
end
[...etc...]
编辑: 以下是管理角色的相同方法:
# just edited Account model with role.
class Account < ActiveRecord::Base
has_many :organizations
has_one :role
end
class Role < ActiveRecord::Base
belongs_to :account
belongs_to :resource, :polymorphic => true
end
class Accountant < ActiveRecord::Base
has_one :role, :as => :resource
end
class Owner < ActiveRecord::Base
has_one :role, :as => :resource
end
EDIT2:
第三次编辑,现在关系是:Account has_many Organizations,每个Organization has_one Role,一个Account has_many Roles through Organizations
class Account < ActiveRecord::Base
has_many :organizations
has_many :roles, :through => :organizations
end
class Role < ActiveRecord::Base
belongs_to :resource, :polymorphic => true
end
class Accountant < ActiveRecord::Base
has_one :role, :as => :resource
end
class Owner < ActiveRecord::Base
has_one :role, :as => :resource
end
是这样吗?
EDIT3:作为非 AR 模型的角色:
这可能取决于您计划拥有多少角色,或者您可能有一个名称:字符串字段,其中指定“会计”、“所有者”等。
如果您不打算将 Role 作为 AR 模型,那么您可以定义一个名为“role_id”的整数列,并在组织模型中使用带有散列的成本:
class Organization < ActiveRecord::Base
belongs_to :account
ROLES={'Accountant' => 1, 'Owner' => 2 }
# get role as literal name
def role
ROLES.invert[role_id]
end
end
这样,您将拥有一个包含多个组织的帐户,每个组织都具有与该特定帐户相关的特定角色。
EDIT4:EDIT3 的代码示例 以下代码是一个 原始 示例,说明了这种关联的外观:
# new account
a = Account.new(....)
# new company & organization with a role
comp = Company.create(....)
org1 = Organization.new(:role_id => 1, ....)
org1.resource = comp
# new Contractor & Organization with another role
contr = Contractor.create(....)
org2 = Organization.new(:role_id => 2, ....)
org2.resource = contr
a.organizations << org1
a.organizations << org2
# save Account, it will have 2 different organizations, each one with a specific role
a.save
【讨论】:
使用多态关联来附加不同类型的组织。类似的东西
class Account < ActiveRecord::Base
has_many :organizations
end
class Organization < ActiveRecord::Base
belongs_to :account
has_many :organizations
end
class Company < ActiveRecord::Base
belongs_to :organization, :polymorphic => true
end
class Contractor < ActiveRecord::Base
belongs_to :organization, :polymorphic => true
end
class Person < ActiveRecord::Base
belongs_to :organization, :polymorphic => true
end
就在我的头上。可能需要一些调整。关于您的角色 - 将 role_id 或角色字符串添加到 Organization 模型。
【讨论】:
【讨论】:
所以我想我找到了最好的解决方案,即使用多表继承,对于任何感兴趣的人,这里有一个链接http://mediumexposure.com/multiple-table-inheritance-active-record/
【讨论】: