Sql Rails将表A连接到表C,其中A有许多B,B有许多C
所以在rails中,我有Sql Rails将表A连接到表C,其中A有许多B,B有许多C,sql,ruby-on-rails,arel,Sql,Ruby On Rails,Arel,所以在rails中,我有 class Organization < ActiveRecord::Base has_many :boxes class Box < ActiveRecord::Base has_many :items belongs_to :organization class Item < ActiveRecord::Base belongs_to :box 类组织
class Organization < ActiveRecord::Base
has_many :boxes
class Box < ActiveRecord::Base
has_many :items
belongs_to :organization
class Item < ActiveRecord::Base
belongs_to :box
类组织
如果不添加组织上的关联,如何查询属于某个组织的所有项目?我不想将组织id添加到项目中。请查看
在您的情况下,您可以将以下内容添加到您的组织
模型中
has_many :items, through: :boxes
然后,您将能够编写如下内容:
组织。通过(id:…,name:…)查找。项目
,似乎完全合理!我在考虑加入,但可能没有必要。谢谢