有趣的铁路协会挑战

时间:2010-07-01 11:15:29

标签: mysql ruby-on-rails activerecord associations

我偶然发现了有关活跃记录关联的有趣挑战:

我有一个帐户模型,可以附加多个不同的组织(例如公司,承包商,人员),并且每个协会(会计师,所有者,查看者等)也有不同的角色。 )。

所以我不确定最好的关联方式。

4 个答案:

答案 0 :(得分:1)

使用多态关联来附加不同类型的组织。像

这样的东西
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模型。

答案 1 :(得分:1)

我正在以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:

thirth编辑,现在的关系是:帐户has_many组织,每个组织has_one角色,帐户has_many角色通过组织

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”的整数列,并在组织模型中使用带有哈希的costant:

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的代码示例 以下代码是 raw 示例,说明了它如何看待这种关联:

# 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

答案 2 :(得分:0)

你看过Polymorphic Associations吗?

答案 3 :(得分:0)

所以我认为我找到了最好的解决方案,即使用多表继承,对于任何感兴趣的人,这里是一个链接http://mediumexposure.com/multiple-table-inheritance-active-record/