如何在两个表上建立has_many关系? [英] how to do a has_many relation on two tables?

查看:84
本文介绍了如何在两个表上建立has_many关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在创建时与一个表建立has_many关系,并将其添加到已创建的另一个表中.

I'm trying to do a has_many relation with a table on creation and also to add it in another table already created.

我的User表已经没有关系了.

I have my User table already with no relations.

我想创建一个具有has_many User关系的Pro_user.

I want to create a Pro_user with a relation has_many User.

问题是,一个User可以具有多个Pro_user,一个Pro_user也可以具有多个User.

The thing is that one User can have multiple Pro_user and a Pro_user can also also have multiple User.

所以两个表都需要has_many对吗?

So both tables need a has_many right ?

所以我想到的是

rails g model pro_user name:string email:string password_digest:string user:references

但这不是正确的事情,它正在将belongs_toPro_user更改为User

But this is not the right thing, it's doing a belongs_to from Pro_user to User

还有如何在现有表User上添加has_many?我是否需要进行迁移以重新创建表并添加关系?

And also how should I do to do add the has_many on my existing table User ? Do I have to do a migration to recreate the table and adding the relation ?

感谢您的帮助!

推荐答案

针对多对多关联的推荐方法是"has_many_through"方法.如果需要更多数据,这使您可以在以后向联接表中添加其他列.您将需要一个联接表,该表至少具有两个对Users和ProUsers表的引用列以及标准id列. (请参阅: http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association )

The recommended approach for a many to many association is the "has_many_through" approach. This allows you to add additional columns later on to the join table if you need more data. You'll need a join table that will at the least have two reference columns to your Users and ProUsers tables along with the standard id column. (Refer to: http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association)

因此,您的User和ProUser表中将没有任何参考列.取而代之的是,制作第三个表,名为BoatsAndPros(随便叫什么),然后执行以下操作:

So your User and ProUser tables will not have any reference columns in them. Instead you'll make a third table called BoatsAndPros (call it whatever you like) and do:

    create_table :boats_and_pros do |t|
      t.belongs_to :user, index: true
      t.belongs_to :pro_user, index: true
      t.timestamps
    end

然后在相应的boats_and_pros.rb模型文件中添加:

Then in the corresponding boats_and_pros.rb Model file you'll add:

belongs_to :user
belongs_to :pro_user

在您的user.rb文件中,您将添加:

In your user.rb file you'll add:

 has_many :boats_and_pros
 has_many :pro_users, through: :boats_and_pros

您将在pro_user.rb模型文件中添加

has_many :boats_and_pros
has_many :users, through: :boats_and_pros

两个要点是:

  • "oldschool" has_and_belongs_to_many方法仍然可以,但是不允许像has_many_through方法那样增加空间,您需要专门命名表pro_users_users,因为Rails希望将这两个表按词法顺序列出
  • 一对多关系(如您在原始尝试中最终遇到的关系)将引用保留在其中一个表中,而多对多关系则需要第三个联接表,如上所示.
  • The "oldschool" has_and_belongs_to_many approach is still fine however doesn't allow room to grow like the has_many_through approach here and you'll need to specifically name the table pro_users_users because Rails expects the two tables to be listed in lexical order.
  • One-to-many relationships like what you ended up with on your original attempt keep the reference in one of the tables while many-to-many relationships require a third join table as shown above.

这篇关于如何在两个表上建立has_many关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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