title: Rails 5: find all Users who belong to Companies, which belong to current_user
body: <p>In my app User can have many Companies and vice versa. In Accounts table id of User and id of its Company is stored. 
  <strong>I want to find all Users who belong to Companies, which belong to current_user.</strong> Let's assume that the current_user is like master User (not Admin, as that would be system Admin) of those companies.
  How do I do this? My guess is to do it with Arel, but then how should it look in Model, Controller, View? Many thanks for any help. I'm on Rails 5.</p>
  
  <p>models/user.rb</p>
  
  <pre><code>class User &lt; ApplicationRecord
  has_many :accounts, dependent: :destroy
  has_many :companies, through: :accounts
  </code></pre>
  
  <p>models/account.rb</p>
  
  <pre><code>class Account &lt; ApplicationRecord
  belongs_to :company
  belongs_to :user
  accepts_nested_attributes_for :company, :user
  </code></pre>
  
  <p>models/company.rb</p>
  
  <pre><code>class Company &lt; ApplicationRecord
  has_many :accounts, dependent: :destroy
  has_many :users, through: :accounts
  accepts_nested_attributes_for :accounts, :users
  </code></pre>
  
  <p>My schema.rb looks like this:</p>
  
  <pre><code>create_table "accounts", force: :cascade do |t|
  t.integer  "company_id"
  t.integer  "user_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["company_id"], name: "index_accounts_on_company_id"
  t.index ["user_id"], name: "index_accounts_on_user_id"
  end
  
    create_table "companies", force: :cascade do |t|
  t.string   "name"
  t.string   "legal_name"
  t.string   "reg_number"
  t.string   "address"
  t.string   "bank_acc"
  t.string   "description"
  t.string   "website"
  t.datetime "created_at",              null: false
  t.datetime "updated_at",              null: false
  t.integer  "role",        default: 0
  t.integer  "currency",    default: 0
  end
  
    create_table "users", force: :cascade do |t|
  t.string   "name"
  t.string   "email"
  t.datetime "created_at",                      null: false
  t.datetime "updated_at",                      null: false
  t.string   "password_digest"
  t.string   "remember_digest"
  t.boolean  "admin",           default: false
  t.index ["email"], name: "index_users_on_email", unique: true
  end
  </code></pre>