别名表名便于3列连接表(MySQL或PostgreSQL) [英] Alias table name to facilitate 3 column join table (MySQL or PostgreSQL)

查看:299
本文介绍了别名表名便于3列连接表(MySQL或PostgreSQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个单独的问题,但它与较早的问题相关:三列加入Rails与活动脚手架。总而言之:Rails自动查找一个两列连接表,但对三列连接表不会这样做。我已经尝试了上一个问题的建议,但它归结为:

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 column join table, but it doesn't do the same for a three column join table. I've tried the suggestions in the previous question, but it comes down to this:

如果你有模型Project,Employee,Role和每个都有一个habtm关系其他两个,rails将单独做每个关系,但不作为一个整体。

If you have the models Project, Employee, Role and each has a habtm relationship to the other two, rails will do 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

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 是否有一种方法来将这些名称别名到真实表名,并仍然允许在数据库(MySQL或PostgreSQL)中的CRUD功能?

My question is this, since rails looks for employees_projects, projects_roles, and employees_roles but not employees_projects_roles is there a way to alias those names to the real table name and still allow CRUD functionality in the database (MySQL or PostgreSQL)?


Whoops。我必须停止回答和公开提问,在我有足够的咖啡。将注释掉的部分从hmt更改为habtm。包含已注释掉部分代码,以反映我尝试的各种选项。

Whoops. I have got to stop answering and questioning publicly before I've had enough coffee. Changed the commented out portion from hmt to habtm. Included commented out portion of code to reflect the various options I've tried.

推荐答案

我假设你有

  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一起使用的连接表的名称。

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

这篇关于别名表名便于3列连接表(MySQL或PostgreSQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆