4

Here is the schema information on my tables:

  • table_name: admin_users, primary_key: id
  • table_name: UserCompanies, primary_key: UserCompanyId, foreign_keys: [CompanyId, UserId]
  • 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. 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. When I run AdminUser.first.companies I get a sql error:

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

So I just need to specify to use UserCompanies.CompanyId on this join. How do I properly specify this foreign key?

2 Answers 2

2

Assuming the TableModule::UserCompany model has these associations...

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
1
  • I tried this and got an error "ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :user_company in model AdminUser" Aug 4, 2015 at 19:59
1

I'm uncertain what you're doing with the TableModule prefixes, but the following should work:

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
2
  • I tried this and got an error "ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :user_companies in model AdminUser" Aug 4, 2015 at 19:59
  • Ya, I actually already tried that and it works, except there is 1 foreign key specification that is still not accurate. Do you know how to specify that one? adding the foreign_key arguement on the companies relationship doesn't do it. ( See my updated question ) Aug 4, 2015 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.