简体   繁体   English

Rails,将has_and_belongs_to_many与抽象模型一起使用

[英]Rails, using has_and_belongs_to_many with abstract Model

I have abstract model Detail that has child models like Carcass , Basis . 我有一个抽象模型Detail ,其中有诸如CarcassBasis类的子模型。 Also i have User model. 我也有User模型。 Can I use has_and_belongs_to_many association between User and Detail ? 我可以在UserDetail之间使用has_and_belongs_to_many关联吗? Could it be inherited to child models? 可以继承到子模型吗? Or do I need to use that association between User and each model of Detal ? 还是我需要在UserDetal每个模型之间使用该关联?

The main goal is to create many-to-many relationship. 主要目标是建立多对多关系。

I am using rails 4. 我正在使用滑轨4。

Sure, has_and_belongs_to_many can be defined only in parent class. 当然,只能在父类中定义has_and_belongs_to_many Child classes will inherit this property 子类将继承此属性

Don't forget to create the respective tables to many to many relationships ( carcass_users and basis_users ) with: 不要忘记通过以下方式创建具有多对多关系( carcass_usersbasis_users )的相应表:

rails g migration CreateCarcassUsers

Inside your migration: 内部迁移:

class CreateCarcassUsers < ActiveRecord::Migration
  def change
    create_table :carcass_users do |t|
      t.integer :carcass_id
      t.integer :user_id
      t.timestamps
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM