简体   繁体   English

Rails 模型,属于很多

[英]Rails Model, belongs to many

I'm having a hard time figuring out how to association one of my models with multiple of another.我很难弄清楚如何将我的一个模型与另一个模型关联起来。

As it is now, I have:就像现在一样,我有:

class ModelA < ActiveRecord::Base
  has_many :model_b
end

class ModelB < ActiveRecord::Base
  belongs_to :model_a
end

However... ModelB needs to belong to not only one instance of ModelA, but possibly three.但是... ModelB 不仅需要属于 ModelA 的一个实例,还可能属于三个。 I know there is a has_many :through, but I'm not sure how it would work in this case.我知道有一个 has_many :through,但我不确定在这种情况下它会如何工作。 EVERY instance of ModelA will always have exactly three instances of ModelB. ModelA 的每个实例将始终只有三个 ModelB 实例。 But as said before, ModelB can belong to more than just one instance of ModelA.但如前所述,ModelB 可以属于多个 ModelA 实例。

Many-to-many relationships in rails don't use belongs_to . rails 中的多对多关系不使用belongs_to Instead, you want to use one of a couple options.相反,您想使用几个选项之一。 The first is has_and_belongs_to_many :第一个是has_and_belongs_to_many

# app/models/category.rb
class Category < ActiveRecord::Base
  has_and_belongs_to_many :items
end

# app/models/item.rb
class Item < ActiveRecord::Base
  has_and_belongs_to_many :categories
end

And you'll need to add an extra join table in your database, with a migration like this:你需要在你的数据库中添加一个额外的连接表,迁移如下:

class AddCategoriesItems < ActiveRecord::Migration
  def self.up
    create_table :categories_items, :id => false do |t|
      t.integer :category_id
      t.integer :item_id
    end
  end

  def self.down
    drop_table :categories_items
  end
end

You can see that the join table's name is the combination of the two other tables' names.您可以看到连接表的名称是其他两个表名称的组合。 The tables must be mentioned in alphabetical order as above, and the :id => false needs to be there, since we don't want a primary key on this table.这些表必须按照上面的字母顺序提及,并且:id => false需要在那里,因为我们不想要这个表上的主键。 It will break the rails association.它将打破 Rails 协会。

There's also another, more complex method known as has_many :through if you need to store info about the relationship itself.还有另一种更复杂的方法,称为has_many :through如果您需要存储有关关系本身的信息。 I've written a whole article detailing how to do both methods, and when to use each:我写了整篇文章,详细介绍了如何使用这两种方法以及何时使用每种方法:

Basic many-to-many Associations in Rails Rails 中的基本多对多关联

I hope this helps, and contact me if you have any other questions!希望能帮到你,还有什么问题可以联系我!

This is what @Jaime Bellmyer used这就是@Jaime Bellmyer 使用的

# app/models/category.rb
class Category < ActiveRecord::Base
  has_and_belongs_to_many :items
end

# app/models/item.rb
class Item < ActiveRecord::Base
  has_and_belongs_to_many :categories
end

I would recommend using this我会推荐使用这个

# app/models/category.rb
class Category < ActiveRecord::Base
  has_many :category_items
  has_many :items, :through => :category_items
end

# app/models/item.rb
class Item < ActiveRecord::Base
  has_many :category_items
  has_many :categories, :through => :category_items
end

# app/models/category_items.rb
class CategoryItems < ActiveRecord::Base
  belongs_to :category
  belongs_to :items
end

If you use this you will have a join model which will give you more control over handling Category and Item.如果您使用它,您将拥有一个连接模型,它可以让您更好地控制处理类别和项目。 But using what @Jaime suggested you will have only a join table and not a model, which will not be under control.但是使用@Jaime 建议您将只有一个连接表而不是一个模型,这将不受控制。

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

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