Ruby-on-rails Rails 模型,属于很多
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4394803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails Model, belongs to many
提问by ardavis
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. I know there is a has_many :through, but I'm not sure how it would work in this case. EVERY instance of ModelA will always have exactly three instances of ModelB. But as said before, ModelB can belong to more than just one instance of ModelA.
但是... ModelB 不仅需要属于 ModelA 的一个实例,还可能属于三个。我知道有一个 has_many :through,但我不确定在这种情况下它会如何工作。ModelA 的每个实例将始终只有三个 ModelB 实例。但如前所述,ModelB 可以属于多个 ModelA 实例。
回答by Jaime Bellmyer
Many-to-many relationships in rails don't use belongs_to. Instead, you want to use one of a couple options. The first is has_and_belongs_to_many:
rails 中的多对多关系不使用belongs_to. 相反,您想使用几个选项之一。第一个是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 => falseneeds to be there, since we don't want a primary key on this table. It will break the rails association.
您可以看到连接表的名称是其他两个表名称的组合。这些表必须按上面的字母顺序提到,并且:id => false需要在那里,因为我们不希望这个表上有主键。它将打破 Rails 协会。
There's also another, more complex method known as has_many :throughif you need to store info about the relationship itself. I've written a whole article detailing how to do both methods, and when to use each:
还有另一种更复杂的方法,has_many :through如果您需要存储有关关系本身的信息。我写了整篇文章,详细介绍了如何使用这两种方法以及何时使用每种方法:
Basic many-to-many Associations in Rails
I hope this helps, and contact me if you have any other questions!
希望能帮到你,还有什么问题可以联系我!
回答by Rohit
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 建议您将只有一个连接表而不是一个模型,这将不受控制。

