简体   繁体   English

模型协会-has_many:through

[英]Association for model - has_many :through

I have three models - Company , User and CompanyUser . 我有三个型号- CompanyUserCompanyUser The associations are as follows. 关联如下。

Company.rb Company.rb

  has_many :company_users
  has_many :users, :through => :company_users

User.rb User.rb

  has_many :company_users, :dependent => :destroy
  belongs_to :company

CompanyUser.rb CompanyUser.rb

  belongs_to :company
  belongs_to :user

For fetching current_user.company, what moddifications are to be made in the model association? 为了获取current_user.company,在模型关联中要进行哪些修改?

Any help would be appreciated. 任何帮助,将不胜感激。

It should be: 它应该是:

has_many :companies, through: :company_users

A has_many :through association is often used to set up a many-to-many connection with another model. has_many:through关联通常用于与另一个模型建立多对多连接。 This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model. 这种关联表明,通过进行第三个模型,可以将声明模型与另一个模型的零个或多个实例匹配。

So if you are creating three models and making a has_many :through association I believe that User will have many Companies and Company will have many Users . 因此,如果您要创建三个模型并创建一个has_many :through关联,我相信User will have many CompaniesCompany will have many Users

But if you need that the user belongs to only one company instead of creating the third model save the company_id in the users table itself. 但是,如果您需要用户仅属于一家company而不是创建第三个模型,则将company_id保存在users表本身中。

Update: 更新:

Now as your scenario is A company can have may users and User belongs to a single company , you need two models: User and Company . 现在,由于您的情况是A company can have may usersUser belongs to a single company ,则需要两个模型: UserCompany Your User model should have an attribute company_id and then company_id should be saved in users table only. 您的User模型应具有属性company_id ,然后company_id应保存在users表中。 Then the associations as follows: 然后的关联如下:

class User < ActiveRecord::Base
  belongs_to :company
end

class Company < ActiveRecord::Base
  has_many :users
end

Then you can do current_user.company 然后你可以做current_user.company

You can get more information on associations in the RailsGuides 您可以在RailsGuides中获得有关关联的更多信息。

According to the associations you have taken, 根据您采取的关联,

user already have as association with the company through the Company User model, so user may have many companies according to your associations. 用户已经通过“公司用户”模型与公司建立了关联,因此根据您的关联,用户可能有很多公司。

so, 所以,

  class User < ActiveRecord::Base

    has_many :company_users, :dependent => :destroy
    has_many :companies, :through => :company_users

  end  

current_user.companies will give you the companies. current_user.companies将为您提供公司。

But if you need only one company for a user then, 但是如果您只需要一家公司供用户使用,

  class User < ActiveRecord::Base
        belongs_to :company
    end  

take belongs_to company and save company_id in users table, 接受belongs_to公司并将company_id保存在用户表中,

then you can call,

`current_user.company`

According to your logic, I think you may need to create a new variable session current_company_user which is object CompanyUser. 根据您的逻辑,我认为您可能需要创建一个新的变量会话current_company_user ,它是对象CompanyUser。 And then, to fetch company by : 然后,通过以下方式获取公司:

current_company_user.company

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM