一种方法可以多次通过

发布于 2024-09-01 18:12:22 字数 509 浏览 7 评论 0原文

我有一个类别、一个子类别和一个产品模型。

我:

Category has_many Subcategories
Subcategory has_many Products
Subcategory belongs_to Category
Product belongs_to Subcategory

有没有办法拥有类似的东西

Category has_many Projects through Subcategories

“正常”rails 方式不起作用,因为“子类别”不属于产品,因此产品没有 subcategory_id 字段。相反,我需要查询类似于

SELECT * FROM products WHERE id IN category.subcategory_ids

是否有办法做到这一点?

谢谢,

尼古拉斯·霍克·伊萨萨

I have a Category, a Subcategory and a Product model.

I have:

Category has_many Subcategories
Subcategory has_many Products
Subcategory belongs_to Category
Product belongs_to Subcategory

Is there a way to have something like

Category has_many Projects through Subcategories

?

The 'normal' rails way wouldn't work because "subcategory" doesn't belongs to product so product does not have a subcategory_id field. Instead, I need the query to be something like

SELECT * FROM products WHERE id IN category.subcategory_ids

Is there a way to do that?

Thanks,

Nicolás Hock Isaza

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2024-09-08 18:12:22

如果您以“正常”Ruby on Rails 方式执行此操作,您描述的数据库将如下所示。如果您的数据库不是这样构造的,我建议您阅读有关如何在 Ruby on Rails 中完成关联的更多信息,因为这是正确的方法(并且您应该在您的数据库中使用 t.references :category迁移,因为它的设计目的是为了让您的参考资料变得更容易)。

+----------------+  +----------------+ +----------------+
| categories     |  | subcategories  | | products       |
+----------------+  +----------------+ +----------------+
| id             |  | id             | | id             |
| ...            |  | category_id    | | subcategory_id |
|                |  | ...            | | ...            |
+----------------+  +----------------+ +----------------+

以此作为您的数据库结构,has_many :products, :through => subcategories 适用于 Category 模型。

Category.rb

class Category < ActiveRecord::Base
  has_many :subcategories
  has_many :products, :through => :subcategories
end

Subcategory.rb

class Subcategory < ActiveRecord::Base
  belongs_to :category
  has_many :products
end

Product.rb

class Product < ActiveRecord::Base
  belongs_to :subcategory
  has_one :category, :through => :subcategory  # don't need this, but it does work
end

ruby script\console

>> c = Category.create
=> #<Category id: 1, ...>
>> c.subcategories.create
=> #<Subcategory id: 1, category_id: 1, ...>
>> p = s.products.create
=> #<Product id: 1, subcategory_id: 1, ...>
>> c.products
=> [#<Product id: 1, subcategory_id: 1, ...>]
>> p.category         # if you have the has_one assocation
=> #<Category id: 1, ...>

If you do this the 'normal' Ruby on Rails way, the database you described would look something like this. If your database isn't structured like this, I suggest reading more about how associations are done for in Ruby on Rails because this is the right way (and you should be using t.references :category in your migration since it was designed to make it easy to not mess your references up).

+----------------+  +----------------+ +----------------+
| categories     |  | subcategories  | | products       |
+----------------+  +----------------+ +----------------+
| id             |  | id             | | id             |
| ...            |  | category_id    | | subcategory_id |
|                |  | ...            | | ...            |
+----------------+  +----------------+ +----------------+

With this as your database structure, the has_many :products, :through => subcategories works for the Category model.

Category.rb

class Category < ActiveRecord::Base
  has_many :subcategories
  has_many :products, :through => :subcategories
end

Subcategory.rb

class Subcategory < ActiveRecord::Base
  belongs_to :category
  has_many :products
end

Product.rb

class Product < ActiveRecord::Base
  belongs_to :subcategory
  has_one :category, :through => :subcategory  # don't need this, but it does work
end

ruby script\console

>> c = Category.create
=> #<Category id: 1, ...>
>> c.subcategories.create
=> #<Subcategory id: 1, category_id: 1, ...>
>> p = s.products.create
=> #<Product id: 1, subcategory_id: 1, ...>
>> c.products
=> [#<Product id: 1, subcategory_id: 1, ...>]
>> p.category         # if you have the has_one assocation
=> #<Category id: 1, ...>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文