简体   繁体   English

如何通过关联在has_many上指定连接表?

[英]How do I specify the join table on a has_many through association?

Here is the schema information on my tables: 这是我的表的架构信息:

  • table_name: admin_users , primary_key: id table_name: admin_users ,primary_key:id
  • table_name: UserCompanies , primary_key: UserCompanyId, foreign_keys: [CompanyId, UserId] table_name: UserCompanies ,primary_key:UserCompanyId,foreign_keys:[CompanyId,UserId]
  • table_name: Companies , primary_key: CompanyId' table_name: Companies ,primary_key:CompanyId'

I want to do something like the following: 我想做类似以下的事情:

AdminUser.first.companies

But, my attempts so far are not working, I'm assuming because I need to specify the table names, model names, or key names, but I don't know how that works with a has_many through relationship. 但是,到目前为止我的尝试都没有用,我假设因为我需要指定表名,模型名或键名,但我不知道如何使用has_many通关系。 Here is my best attempt at defining it so far: 到目前为止,这是我最好的定义:

class AdminUser < ActiveRecord::Base
    has_many :user_companies, class_name:"TableModule::UserCompany", foreign_key:"UserId"
    has_many :companies, through: :user_companies, class_name: "TableModule::Company"
end

# this code is from a rails engine separate from the app where AdminUser is defined
# the purpose of the engine is to provide access to this particular database 
# the CustomDBConventions class adapts the models for this database to work with ActiveRecord so we can use snake case attributes, reference the primary key as 'id', and it specifies the correct tables names.
module TableModule
    class UserCompany < CustomDBConventions
        belongs_to :admin_user
        belongs_to :company
    end

    class Company < CustomDBConventions
        has_many :admin_users, through: :user_companies
    end

    class CustomDBConventions < ActiveRecord::Base
        self.abstract_class = true
        def self.inherited(subclass)
            super
            subclass.establish_connection "table_module_#{Rails.env}".to_sym
            tb_name = subclass.table_name.to_s.gsub(/^table_module_/,"").classify.pluralize
            subclass.table_name = tb_name
            subclass.primary_key = tb_name.singularize + "Id"
            subclass.alias_attribute :id, subclass.primary_key.to_sym
            subclass.column_names.each do |pascal_name|
                subclass.alias_attribute pascal_name.underscore.to_sym, pascal_name.to_sym
                subclass.alias_attribute "#{pascal_name.underscore}=".to_sym, "#{pascal_name}=".to_sym
            end
        end
    end
end

EDIT: So this setup is really close and I am missing only 1 foreign key specification. 编辑:所以这个设置非常接近,我只缺少1个外键规范。 When I run AdminUser.first.companies I get a sql error: 当我运行AdminUser.first.companies时,我收到一个SQL错误:

TinyTds::Error: Invalid column name 'company_id'.: EXEC sp_executesql N'SELECT [Companies].* FROM [Companies] INNER JOIN [UserCompanies] ON [Companies].[CompanyId] = [UserCompanies].[company_id] WHERE [UserCompanies].[UserId] = @0', N'@0 int', @0 = 1 TinyTds ::错误:列名'company_id'无效:EXEC sp_executesql N'SELECT [公司]。* FROM [公司] INNER JOIN [UserCompanies] ON [Companies]。[CompanyId] = [UserCompanies]。[company_id] WHERE [用户公司]。[UserId] = @ 0',N'@ 0 int',@ 0 = 1

So I just need to specify to use UserCompanies.CompanyId on this join. 所以我只需要指定在此连接上使用UserCompanies.CompanyId。 How do I properly specify this foreign key? 如何正确指定此外键?

Assuming the TableModule::UserCompany model has these associations... 假设TableModule :: UserCompany模型具有这些关联...

class TableModule::UserCompany < ActiveRecord::Base
  belongs_to :admin_user
  belongs_to :company
end

...then I think this is what you're after: ...那么我认为这就是你所追求的:

class AdminUser < ActiveRecord::Base
  has_many :companies, through: :user_company, class_name: "TableModule::UserCompany"
end

I'm uncertain what you're doing with the TableModule prefixes, but the following should work: 我不确定你在使用TableModule前缀做了什么,但以下内容应该有效:

class AdminUser < ActiveRecord::Base
  has_many :user_companies
  has_many :companies, through: :user_companies
end

class Company < ActiveRecord::Base
  has_many :user_companies
  has_many :admin_users, through: :user_companies
end

class UserCompany < ActiveRecord::Base
  belongs_to :admin_user
  belongs_to :comany
end

暂无
暂无

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

相关问题 我是否需要has_many的联接表:通过关联? - Do I need a join table for a has_many :through association? 如何基于联接表为has_many:through关联设置default_scope? - How do I set a default_scope for a has_many :through association based on the join table? 如何通过在rails中通过关联对has_many进行关联来实现has_many? - How do I do a has_many through association on a has_many through association in rails? 如何在has_many:through关联中访问联接模型的属性 - How do I access attributes of the join model in a has_many :through association 如何通过关联更新Has_Many的记录? - How do I update the record of a Has_Many through association? 如果我在Ruby on Rails中指定了has_many:through关联,是否需要为第三张表创建模型? - If I specify a has_many :through association in Ruby on Rails, do I have to create a model for the 3rd table? 如何通过与多个作用域关联来消除has_many中同一表的多个联接? - How do I eliminate multiple joins of the same table in a has_many through association with multiple scopes? 通过以下方式在has_many上优雅地设置联接表属性: - Elegantly set join table attributes on has_many through: association 在视图中显示联接表行(has_many通过关联) - Displaying join table row in view (has_many through association) 通过Rails 4中的关联,联接表和has_many出现问题 - Issue with join table and has_many through association in Rails 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM