【问题标题】:Ruby on Rails: has_many self joinRuby on Rails:has_many 自加入
【发布时间】:2018-01-24 06:51:47
【问题描述】:

导轨 5。

我一直在尝试根据本教程创建一个自引用的has_many关系:https://medium.com/@jbmilgrom/active-record-many-to-many-self-join-table-e0992c27c1e(最后一段:Has_many | Has_many — Self Join Table)

我在这里也看到了关于这个概念的各种线程,但是当谈到我的问题及其术语时,我仍然有点迷茫。而且我不确定数据库应该是什么样子。我还应该提到我对 Rails 很陌生。

我正在尝试在用户之间创建“代表”-“代表”关系。

一个用户可以由多个用户代表(User has_many代表)。 而一个User可以代表很多User(User has_many Represented)。

根据上面链接中的教程,我编写了这样的用户模型:

class User < ApplicationRecord
  has_many :representatives, through: :representive_link, source: :representative
  has_many :representive_link, foreign_key: :represented_user_id, class_name: 'representatives'

  has_many :represented, through: :represented_link, source: :represented
  has_many :represented_link, foreign_key: :representative_user_id, class_name: 'representatives'
end

然后我像这样创建了一个代表表(我称它为代表,但它实际上是代表组和代表):

class Representatives < ActiveRecord::Base
  belongs_to :representative, foreign_key: 'representative_user_id', class_name: 'User'
  belongs_to :represented, foreign_key: 'represented_user_id', class_name: 'User'
end

我还在数据库中创建了一个代表表,如下所示:

+----------------------+------------+------+-----+---------+----------------+
| Field                | Type       | Null | Key | Default | Extra          |
+----------------------+------------+------+-----+---------+----------------+
| id                   | bigint(20) | NO   | PRI | NULL    | auto_increment |
| representive_user_id | bigint(20) | YES  | MUL | NULL    |                |
| represented_user_id  | bigint(20) | YES  | MUL | NULL    |                |
| created_at           | datetime   | NO   |     | NULL    |                |
| updated_at           | datetime   | NO   |     | NULL    |                |
+----------------------+------------+------+-----+---------+----------------+

所以,我在 Rails 控制台中创建了两个用户:

u1 = User.create('id' => 1, 'username' => 'john')
u2 = User.create('id' => 2, 'username' => 'mike')

我想让 u2 代表 u1:

u1.representatives = [u2]

但我收到此错误:

NameError: uninitialized constant User::representatives

我发现这个概念很混乱,这里的其他线程并没有为我解释清楚。有人可以根据我的问题打破这个概念吗?

【问题讨论】:

标签: ruby rails-activerecord ruby-on-rails-5 has-many-through has-many


【解决方案1】:

好的,最后我通过一些细微的修改让它工作了。首先,我重命名了一些东西以使其更有意义(如果只是在我的脑海中)。

用户模型:

class User < ApplicationRecord
  has_many :represented_link, foreign_key: :representative_id, class_name: 'RepresentativeGrouping'
  has_many :represented, through: :represented_link, source: :represented

  has_many :representive_link, foreign_key: :represented_id, class_name: 'RepresentativeGrouping'
  has_many :representatives, through: :representive_link, source: :representative
end

RepresentativeGrouping 模型:

class RepresentativeGrouping < ApplicationRecord
  belongs_to :representative, foreign_key: 'representative_id', class_name: 'User'
  belongs_to :represented, foreign_key: 'represented_id', class_name: 'User'
end

representative_grouping 表:

mysql> describe representative_groupings;
+-------------------+------------+------+-----+---------+----------------+
| Field             | Type       | Null | Key | Default | Extra          |
+-------------------+------------+------+-----+---------+----------------+
| id                | bigint(20) | NO   | PRI | NULL    | auto_increment |
| representative_id | bigint(20) | YES  | MUL | NULL    |                |
| represented_id    | bigint(20) | YES  | MUL | NULL    |                |
| created_at        | datetime   | NO   |     | NULL    |                |
| updated_at        | datetime   | NO   |     | NULL    |                |
+-------------------+------------+------+-----+---------+----------------+

这里至关重要的一件事是在 User 模型中安排了 has _many 定义。在关于您的问题中,您将看到带有 through: 和 source: 的 has_many 定义位于带有 class_name: 和 foreign_key: 的 has_many 定义之下。在我改变了周围的名字后,我遇到了我的第一个错误:

NameError: uninitialized constant User::representatives

但转移到另一个错误:

ActiveRecord::HasManyThroughOrderError: Cannot have a has_many :through association 'User#representatives' which goes through 'User#representive_link' before the through association is defined.

谷歌搜索把我带到了这里:https://github.com/rails/rails/issues/29123,有人提到了定义排列的问题,你必须按照我的顺序排列它们。

在这些更改之后,我能够运行如下代码:

u1 = User.create(:id => 1, :username => 'john')
u2 = User.create(:id => 2, :username => 'mike')

u1.representatives = [u2]
puts u1.representatives

### displays u2's data

puts u2.represented

### displays u1's data

【讨论】:

    【解决方案2】:

    在用户模型中

    要获得代表 - has_and_belongs_to_many :representatives, class_name: "User", join_table: : representative_groupings, foreign_key: :represented_id, association_foreign_key: : representative_id

    要获得 represented_by - has_and_belongs_to_many : represented, class_name: "User", join_table: : representative_groupings, foreign_key: :representative_id, association_foreign_key: : represented_id

    这也消除了对显式模型的需要 rails - 代表分组

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 1970-01-01
      相关资源
      最近更新 更多