Table alias name to facilitate 3 column join column (MySQL or PostgreSQL)

This is a separate question, but it is related to an earlier question: Three columns Join Rails with an active scaffold . To summarize: Rails automatically looks for a join table with two columns, but it does not do the same for a join table with three columns. I tried the sentences in the previous question, but it comes down to the following:

If you have Project, Employee, Role models and everyone has a habtm relation to the other two, the rails will fulfill each relationship separately, but not as a whole.

class Employee < ActiveRecord::Base
    #has_many        :employees_projects_roles
    #has_many        :roles,     :through => :employees_projects_roles
    #has_many        :projects,  :through => :employees_projects_roles
     has_and_belongs_to_many :roles
     has_and_belongs_to_many :projects
end

repeated for Project, Role follows

class Role < ActiveRecord::Base
    #has_many :employees, :through => :employees_projects_roles
    #has_many :projects,  :through => :employees_projects_roles
    has_and_belongs_to_many :employees
    has_and_belongs_to_many :projects
end

, , rails employees_projects, projects_roles, employees_roles, employees_projects_roles, - CRUD (MySQL PostgreSQL )?

[] . , . hmt habtm. , , .

+3
1

, - , :

  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

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
+6

Source: https://habr.com/ru/post/1717483/


All Articles