Ruby on rails Rails:3个键的复杂HABTM关系?
我试图确定一个特定组织有多少会议时间,但由于某些关系的复杂性,这并不像看上去那么容易Ruby on rails Rails:3个键的复杂HABTM关系?,ruby-on-rails,has-and-belongs-to-many,Ruby On Rails,Has And Belongs To Many,我试图确定一个特定组织有多少会议时间,但由于某些关系的复杂性,这并不像看上去那么容易 class Meeting < ActiveRecord::Base has_and_belongs_to_many :people end class Person < ActiveRecord::Base has_any_belongs_to_many :meetings has_and_belongs_to_many :positions end class Pos
class Meeting < ActiveRecord::Base
has_and_belongs_to_many :people
end
class Person < ActiveRecord::Base
has_any_belongs_to_many :meetings
has_and_belongs_to_many :positions
end
class Position < ActiveRecord::Base
has_and_belongs_to_many :people # b/c over time more than one person
may hold this position over time
belongs_to :organization
end
class Organization < ActiveRecord::Base
has_many :positions
end
课堂会议
因为人们可能会随着时间的推移而改变立场(和组织),当有人参加会议时,我还需要记录他们当时的立场,以便统计准确
目前,由于HATBM联接表是隐式处理的(meetings\u people),所以我无法跟踪位置。如何将职位添加到这个组合中,以及编写查询以获取每个组织的总会议时间的最佳方式是什么
提前感谢您提供的任何帮助或建议
有许多:通过
允许您使用适当的连接模型,而不是像habtm
那样的隐含连接模型。您可以创建一个名为MeetingAttention
的整个类,在其中存储会议和与会者。然后,您可以在模型上放置所需的任何属性,包括position
。然后简单地
Person has_many :meeting_attendances
Person has_many :meetings, :through => :meeting_attendances
Meeting has_many :meeting_attendances
Meeting has_many :people, :through => :meeting_attendances
及
课堂会议出勤
注意:您可能需要对此进行调整,因为我不确定Rails如何处理“Person”的多元化,但这是一般的想法
class MeetingAttendance < ActiveRecord::Base
belongs_to :person
belongs_to :meeting
end