ruby-on-rails 如何通过has_many关联覆盖ActiveRecord关系

tzdcorbm  于 2023-11-20  发布在  Ruby
关注(0)|答案(1)|浏览(122)

所以我有一定的冰淇淋,我想得到的销售额在安大略的低脂冰淇淋和非过期,说。
IceCream有_多个安大略_sales_receipts:

included_ice_cream = IceCream.produced_this_or_previous_month.where(low_fat: true, expired: false)

final_amount = included_ice_cream.ontario_sales_receipts.pluck(:number_of_buckets).sum * BUCKET_PRICE

字符串
但这会导致:

undefined method `ontario_sales_receipts' for #<IceCream::ActiveRecord_Relation:0x0055de80e3ffa8>


如何从ActiveRecord关系中提供has_many关联?
我最初可以查询OntarioSaleRecept,但我不知道如何根据它们与特定IceCream的has_one关联来查询它们

included_ontario_sales_receipts = OntarioSalesReceipt.where( their associated IceCream is lowfat and non expired ... )

hgb9j2n6

hgb9j2n61#

included_ice_cream = IceCream.produced_this_or_previous_month.where(
      low_fat: true,
      expired: false
    )
    total_amount = final_amount(included_ice_cream)

    def final_amount(included_ice_cream)
      included_ice_cream.map do |ice_cream|
        ice_cream.ontario_sales_receipts.pluck(:number_of_buckets).sum *
          BUCKET_PRICE
      end.sum
    end

字符串
这个怎么样?

相关问题