title: Different Relationships with the same model
body: <p>I've 3 models; <strong>User</strong> , <strong>Group</strong> and <strong>GroupMap</strong>.</p>
  
  <p>Users can have multiple groups and groups have multiple users. This is n-m relationship and it is done via GroupMap. GroupMap also have status, and type so I need this model as well. This is the first relationship.</p>
  
  <p>A group can only have one owner which is user. this is 1-n relationship.</p>
  
  <p><strong>user.rb</strong></p>
  
  <pre><code>class User &lt; ApplicationRecord
  
    has_many :group_maps
    has_many :groups, :through =&gt; :group_maps
  </code></pre>
  
  <p><strong>group.rb</strong></p>
  
  <pre><code>class Group &lt; ApplicationRecord
  
    belongs_to :user
  
    has_many :group_maps
    has_many :users, :through =&gt; :group_maps
  </code></pre>
  
  <p><strong>group_map.rb</strong></p>
  
  <pre><code>class GroupMap &lt; ApplicationRecord
    belongs_to :group
    belongs_to :user
  </code></pre>
  
  <p><strong>groups_controller.rb</strong></p>
  
  <pre><code>class GroupsController &lt; ApplicationController
  
    def new
      @group = Group.new
    end
  
    def create
      @group = current_user.groups.create(group_params)
  
      if @group.save
        redirect_to root_path
      else
        render 'new'
      end
    end
  </code></pre>
  
  <p>Although I can create groups with this code there are 2 problems here;</p>
  
  <ol>
  <li>user_id in Group model which is to store owner of it is always nil although in GroupMap model it set's the user_id properly. </li>
  <li>In step 1, it is good to see owner in GroupMap too since its a member of that group too but its status is always nil. There are 3 type of status(waiting,accepted,reject). In this case, when a owner creates that group, its status with the group must be accepted as well.</li>
  </ol>
  
  <p><strong>log</strong></p>
  
  <pre><code>(0.0ms)  begin transaction
    SQL (1.0ms)  INSERT INTO "groups" ("name") VALUES (?)  [["name", "Football lovers"]]
    SQL (0.5ms)  INSERT INTO "group_maps" ("group_id", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["group_id", 8], ["user_id", 4], ["created_at", 2017-03-01 19:03:55 UTC], ["updated_at", 2017-03-01 19:03:55 UTC]]
  </code></pre>