简体   繁体   English

Rails 5:查找属于公司的所有用户,这些用户属于 current_user

[英]Rails 5: find all Users who belong to Companies, which belong to current_user

In my app User can have many Companies and vice versa.在我的应用程序中,用户可以拥有许多公司,反之亦然。 In Accounts table id of User and id of its Company is stored.在 Accounts 表中存储了用户的 id 和公司的 id。 I want to find all Users who belong to Companies, which belong to current_user.我想找到属于公司的所有用户,这些用户属于 current_user。 Let's assume that the current_user is like master User (not Admin, as that would be system Admin) of those companies.让我们假设 current_user 就像这些公司的主用户(不是管理员,因为那将是系统管理员)。 How do I do this?我该怎么做呢? My guess is to do it with Arel, but then how should it look in Model, Controller, View?我的猜测是用 Arel 来做,但是它在模型、控制器、视图中应该如何看? Many thanks for any help.非常感谢您的帮助。 I'm on Rails 5.我在 Rails 5 上。

models/user.rb模型/用户.rb

class User < ApplicationRecord
has_many :accounts, dependent: :destroy
has_many :companies, through: :accounts

models/account.rb模型/帐户.rb

class Account < ApplicationRecord
belongs_to :company
belongs_to :user
accepts_nested_attributes_for :company, :user

models/company.rb模型/公司.rb

class Company < ApplicationRecord
has_many :accounts, dependent: :destroy
has_many :users, through: :accounts
accepts_nested_attributes_for :accounts, :users

My schema.rb looks like this:我的 schema.rb 看起来像这样:

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

You can find current user's companies, and eager load users who belong to those companies您可以找到当前用户的公司,以及属于这些公司的急切加载用户

@companies = current_user.companies.includes(:users)

To list all users(may be in a view), loop through @companies and all its users要列出所有用户(可能在一个视图中),循环遍历@companies及其所有users

@companies.each do |company|
  company.users.each do |user|
    puts user.name
  end
end

Or use map/collect to assign them to a variable.或者使用 map/collect 将它们分配给一个变量。

@users = @companies.map(&:users).flatten

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails和Active Record:查找其子代不属于current_user或没有任何子代的对象 - Rails & Active Record: Find objects whose children don't belong to current_user OR who don't have any children 除非当前用户是属于记录的用户之一 - Unless current user is one of the users who belong to a record 只显示索引操作中属于current_user的任务? - Only display tasks that belong to the current_user in the index action? Rails查找属于类别集合的所有产品 - Rails find all products which belong to a collection of categories 如何在Rails上的ruby中显示除当前用户和current_user的朋友以外的所有用户 - how can I display all users except current user and current_user's friends in ruby on rails Rails计算属于用户的所有帖子的页面视图 - Rails count page views of all the posts that belong to a user 复杂的named_scope:查找不属于某个项目的用户 - Complicated named_scope: Find users who don't belong to a certain project Rails:选择除current_user以外的随机用户 - Rails: selecting random users except current_user 如何仅在Ruby on Rails 3中将current_user限制为已确认的用户? - How to restrict current_user to confirmed users only in Ruby on Rails 3? Rails,API端点,返回所有current_user是卖方的商品? - Rails, API endpoint that returns all Items for which the current_user is the seller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM