[英]Table Associations in Ruby on Rails
There are two tables, groups, and groups_hierarchy. 有两个表,组和groups_hierarchy。 groups has standard information about a group and the group_hierarchy has two columns (parent, child) that list the parent group's id and child group's id.
groups具有有关组的标准信息,group_hierarchy具有两列(父级,子级),其中列出了父组的ID和子组的ID。 This is to say that the child group is a subgroup of the parent group.
也就是说,子组是父组的子组。 I have been trying to figure out what the associations would be in the GroupHierarchy and Group Data Models.
我一直在尝试找出GroupHierarchy和Group Data模型中的关联。 Can someone help me with this?
有人可以帮我弄这个吗?
A group can have many subgroups and it can be a subgroup of many other groups. 一个组可以具有许多子组,并且可以是许多其他组的子组。 I figured it would be a
has_many :grouphierarchies
in Group and belongs_to :group
in GroupHierarchy but that didn't work...the thing is GroupHierarchy technically belongs to 2 groups. 我认为这将是Group中的
has_many :grouphierarchies
和has_many :grouphierarchies
中的belongs_to :group
,但是那行不通...从技术上讲,GroupHierarchy属于2个组。
Thanks 谢谢
In my Group Model I needed the following: 在我的小组模型中,我需要以下内容:
class Group < ActiveRecord::Base
has_and_belongs_to_many :children, :class_name => 'Group', :join_table => 'groups_hierarchy', :foreign_key => 'parent', :association_foreign_key => 'child' has_and_belongs_to_many :parents, :class_name => 'Group', :join_table => 'group_hierarchy', :foreign_key => 'child', :association_foreign_key => 'parent'
has_and_belongs_to_many :children, :class_name => 'Group', :join_table => 'groups_hierarchy', :foreign_key => 'parent', :association_foreign_key => 'child' has_and_belongs_to_many :parents, :class_name => 'Group', :join_table => 'group_hierarchy', :foreign_key => 'child', :association_foreign_key => 'parent'
end
And you actually don't need anything in the GroupHierarchy class, which seems to just act as a support table to allow for self-referencing. 实际上,您在GroupHierarchy类中不需要任何东西,它似乎只是作为支持表来允许自引用。
Thanks again 再次感谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.