我有模型:

class Order < ApplicationRecord
  acts_as_paranoid
  has_paper_trail

  enum status: %i[created in_process]

  has_many :order_containers
  has_many :line_items
end

class LineItem < ApplicationRecord
  acts_as_paranoid
  has_paper_trail

  enum status: %i[in_process collected]

  belongs_to :order
  belongs_to :variant
end

class Variant < ApplicationRecord
  acts_as_paranoid
  has_paper_trail

  has_many :line_items

  belongs_to :product

  validates :barcode, presence: true
end

class Product < ApplicationRecord
  acts_as_paranoid
  has_paper_trail

  belongs_to :isles, required: false
  has_many :variants

  validates :name, :volume, :sku, :price, presence: true
end

class Isle < ApplicationRecord
  acts_as_paranoid
  has_paper_trail

  has_many :products
  validates :name, presence: true
end

我只需要输出产品属于特定岛屿的订单.例如,如果订单中没有属于我需要的岛屿的产品,则不需要显示此订单及其产品.如果订单中有属于特定岛屿的产品,例如(isle.id 1),则需要撤回此类订单,并撤回属于该部门的产品

我试着这样做:

@products = Order.includes([:line_items, :variants, :products, :isles]).where('products.isle_id = isle.id').references(:orders)

但我有个错误:

ailure/Error: return { "#{root_name}": [] } if records.blank?
 
 ActiveRecord::StatementInvalid:
   PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "products"
   LINE 1: ..."orders" WHERE "orders"."deleted_at" IS NULL AND (products.i...

如果我设计得不好,我很抱歉,我是一个新手开发人员,这是我的第一个任务)

推荐答案

这将按订单1从Isle 1退回所有产品.如果订单具有来自同一产品的多个变体,它将返回重复的产品,如果这不是您需要的,则在这些查询中添加.distinct.

>> order = Order.first
>> isle  = Isle.first

>> Product.joins(variants: { line_items: :order }).where(isle_id: isle, line_items: { order_id: order })
=> [#<Product:0x00007f1551fc4820 id: 1, isle_id: 1>,
    #<Product:0x00007f1551fc4258 id: 2, isle_id: 1>]

您可以在Order中添加一些关联,以简化此操作:

class Order < ApplicationRecord
  has_many :line_items

  has_many :variants, through: :line_items
  has_many :products, through: :variants
end
>> Order.first.products.where(isle_id: Isle.first)
=> [#<Product:0x00007f154babcb30 id: 1, isle_id: 1>, 
    #<Product:0x00007f154babca18 id: 2, isle_id: 1>]

Update

确保正确创建关联.在控制台中使用create!save!方法来引发任何验证错误.

# NOTE: create a new order with two products in two different isles
#       just add the required attributes from your models.

order = Order.create!(line_items: [
  LineItem.new(variant: Variant.new(product: Product.new(isle: (isle = Isle.create!)))),
  LineItem.new(variant: Variant.new(product: Product.new(isle: Isle.new)))
])

# NOTE: verify that you have two products

>> order.products
=> [#<Product:0x00007f6f1cb964e0 id: 1, isle_id: 1>,
    #<Product:0x00007f6f1cb963f0 id: 2, isle_id: 2>]

# NOTE: filter by isle

>> order.products.where(isle_id: isle)
=> [#<Product:0x00007f6f1ccda630 id: 1, isle_id: 1>]

>> order.products.where(isle_id: 2)
=> [#<Product:0x00007f6f1d140cd8 id: 2, isle_id: 2>]

100

100

Ruby-on-rails相关问答推荐

如何测试自定义路由?

如何控制或定制S为模型使用的命名路由Rail()?

Rails Heroku部署:预编译assets资源 失败.SassC::语法错误

如何断言 Ruby 单元测试中的错误消息?

如何启动 rails 控制台并专门使用测试数据库?

Rails cron 与时俱进,设置环境

为什么使用 HTTP PUT 和 DELETE 方法而不是 POST?

使用 javascript 提交 Rails 远程表单

在 Rails 中,如何处理多个选中的复选框,只需拆分 , 或?

RSpec > 有没有办法用一个命令运行所有测试?

搭建脚手架时建立关系

如何在 routes.rb 中使用 301 或 302 进行重定向

在 Ruby on Rails 中循环对象属性

Rails Devise:设置密码重置令牌并重定向用户

Rails SQL 正则表达式

在 Rails 路由中更改 id 参数

Active Record has_many:通过删除一条关联记录

为什么 RSpec 在 Rails 下这么慢?

rails 获取应用程序根/基本 url

Ruby 安装的 RVM 不工作?