Alias table name to facilitate 3 column join table (MySQL or PostgreSQL)

前端 未结 1 1236
暗喜
暗喜 2021-01-14 17:00

This is a separate question, but it is related to an earlier question: Three Column Join in Rails with Active Scaffold. To sum up: Rails automatically looks for a two colum

相关标签:
1条回答
  • 2021-01-14 17:30

    I'm assuming that you have something like this joining your models together:

      def self.up
        create_table :my_join_table, :id => false do |t|
          t.integer :employee_id
          t.integer :role_id
          t.integer :project_id
          t.timestamps
        end
      end
    

    If so you, simply need to specify the name of the join table to use with your habtm.

    class Employee < ActiveRecord::Base
      has_and_belongs_to_many :roles, :join_table => "my_join_table"
      has_and_belongs_to_many :projects, :join_table => "my_join_table"
    end
    
    class Project < ActiveRecord::Base
      has_and_belongs_to_many :roles, :join_table => "my_join_table"
      has_and_belongs_to_many :employees, :join_table => "my_join_table"
    end
    
    class Role < ActiveRecord::Base
      has_and_belongs_to_many :employees, :join_table => "my_join_table"
      has_and_belongs_to_many :projects, :join_table => "my_join_table"
    end
    
    0 讨论(0)
提交回复
热议问题