I am trying to figure out how to do a self referencing association in Rails. I'm a Rails beginner.

Basically, I have a model `Group`. Each Group can have many sub-groups. I feel like I've tried everything, but I can't get the join to work.

What I have now is 

```
# GroupSubGroup Model
class GroupSubGroup < ApplicationRecord
  belongs_to :group
  belongs_to :sub_group, class_name: 'Group'
end

```

and then my Group Model looks like 

```
     has_many :group_sub_groups
     has_many :sub_groups, foreign_key: :sub_group_id, through: :group_sub_groups, class_name: 'GroupSubGroup'
     has_many :groups, through: :sub_groups
     has_many :groups, class_name: 'GroupSubGroup'

```

And my migration looks like 

```
    create_table :group_sub_groups do |t|
      t.integer :group_id, index: true, foreign_key: { to_table: :groups }
      t.references :sub_group, index: true, foreign_key: { to_table: :groups }

      t.timestamps
    end

```

My main issue is that I can add a new `GroupSubGroup` row into the join table using `parent_group.sub_groups.new`, however when I retrieve the parent group and loop over it's `sub_groups`, none of the instances are of the `Group` class and therefore don't have any of the methods. 

For example

```
Group.all.each do |group|
      group.sub_groups.each do |s|
        puts "#{s.name} is a sub group for #{group.name}"
      end
    end

```

Throws an `undefined method 'name'` error.