A method that shows maps users are a part of A little background: 

My app has two models called mapgroups and usergroups. A map belongs to a group and a user belongs to group. You can go in a group and add maps which is how maps are "assigned" to a user. These will show up on their dashboard.

class User &lt; ApplicationRecord
  def fetch_maps
    @groups = self.groups
    @maps = Array.new

    @groups.each do |g|
      g.maps.each do |m|
        @maps &lt;&lt; m
      end
    end

    return @maps
  end
end


In order to display the maps the user should have shown in their dashboard I created a fetch_maps method to call on the current user. 

Im wondering if there is any way to clean this up or really just any feedback. This is the first method I have written with current Ruby knowledge.

Edit:

Mapgroup Model

class Mapgroup &lt; ApplicationRecord
  belongs_to :map, optional: true
  belongs_to :group, optional: true
end


Usergroup Model

class Usergroup &lt; ApplicationRecord
  belongs_to :user, optional: true
  belongs_to :group, optional: true
end
This would potentially be a very expensive way to do this as you have to query the dtabase once for every group the user is in, i.e. an n+1 query. You should be able to do it with a single query by creating a relation:

class User &lt; ApplicationRecord
  belongs_to :usergroup
  has_many   :groups,     through: :usergroup
  has_many   :maps,       through: :groups


then you can just query the map relation.  An alternative would be to use a join. There is more than one way to do this but you could do:

def fetch_maps
  Map.joins(:mapgroups =&gt; {:groups =&gt; :users} ).where(user: self)
end


Rails doesn't always run the queries the way you expect so I suggest you run your code in the console or watch your log files to make sure that you are only running one query.