在has_many:through中,创建对象继承的正确方法是什么

时间:2011-06-13 17:28:03

标签: ruby-on-rails activerecord

我遇到了一些我不理解如何使用Rails关联建模的东西,STI和多态似乎都没有解决它。

我希望能够通过has_many:through创建的集合访问连接表中的属性。

在下面的代码中,这意味着我希望能够通过.members集合中的对象访问委员会职位的名称和描述,但据我所知,我不能这样做。我必须通过原始的连接表。

e.g。为俱乐部及其委员会成员建模

class User < ActiveRecord::Base
  attr_accessible :full_name, 
                  :email
  has_many: committee_positions
  has_many: committees, :through => committee_positions
end

class Committee < ActiveRecord::Base
  attr_accessible :name

  has_many :committee_positions
  has_many :members, :through => :committee_positions
end

class CommitteePosition < ActiveRecord::Base
  attr_accessible :user_id, 
                  :committee_id, 
                  :member_description, 
                  :role_title

  belongs_to :committee
  belongs_to :user
end

假设每个委员会职位实例都有唯一的描述 即该描述特别适用于会员和委员会,因此必须存储在联接表中,而不是存储在用户或俱乐部中。

e.g。

Committee member:  Matt Wilkins
Role:             "Rowing club president"
Description:      "Beats the heart of the rowing club to his own particular drum"

有没有办法通过committee.members集合访问连接表中的数据?

虽然活动记录为我们提供了直接转发给成员的优秀别名,但似乎没有办法访问创建该集合的连接表上的数据:

我无法做到以下几点:

rowing_committee.members.find_by_role_title('president').name

.members集合中的每个项目都是一个用户对象,似乎无法访问存储在CommissionPositions连接表中的角色或描述。

这样做的唯一方法是:

rowing_committee.committee_positions.find_by_role_title('president').user.name

这是完全可行的但是笨重而且无益。我觉得用例足够通用,我可能会遗漏一些东西。

我想通过委员会收集的内容访问<。strong>

member
- full_name
- email
- role_title (referenced from join table attributes)
- member_description (referenced from join table attributes)

这只是一件小事,但感觉很难看。是否有一种干净的方法来指示“成员”对象继承连接表中包含的信息?

--------------附录

在完成这项工作后,我意识到我可以通过简单地为委员会成员定义一个新类并在has_many:through关系中引用而不是用户来解决问题。它工作得更好但仍然很笨重

class Committee < ActiveRecord::Base
  ...

  has_many :committee_positions
  has_many :members, 
           :through => :committee_positions, 
           :class_name => 'CommitteeMember'
  ...
end


class CommitteeMember < User

  def description( committee )
    self.committees.find_by_committee_id( committee.id ).description
  end

  def role( committee )
    self.committees.find_by_committee_id( committee.id ).description
  end
end

现在这种情况越来越近了但是使用它的代码仍然是笨重的:

committee = Committee.first
president_description = committee.members.find_by_title('president').description( committee )

有没有办法用他们引用的委员会初始化这些对象?

1 个答案:

答案 0 :(得分:1)

我想你可以在这里使用一些代表团。在您的Committee_Position类中:

class Committee_Position < ActiveRecord::Base
  attr_accessible :user_id, 
                :committee_id, 
                :member_description, 
                :role_title

belongs_to :committee
belongs_to :user

delegate :name, :email, :to => :user
end

所以你可以做你想说的话:

rowing_club.committee_members.find_by_role_title('president').name
相关问题