有两种模式:折扣和TimeDiscount。
折扣has_one TimeDiscount和TimeDiscount belongs_to折扣。
我需要得到所有的TimeDiscounts和相关的Discount.is_enabled等于true。我该怎么做呢?我知道范围界定,我可以做一些类似的事情:
scope :new_orders, -> { where(order_status: OrderStatus.new_order) } 请告诉我如何利用这里的社团。
发布于 2014-02-28 06:43:59
Discount.where(is_enabled: true).map do |discount|
discount.time_discounts
end这将返回一个与您的需求匹配的TimeDiscount数组。
发布于 2014-02-28 07:11:46
像下面这样的东西?
TimeDiscount.includes(:discount).where(discount: {is_enabled: true})我相信这也是可行的:
TimeDiscount.includes(:discount).merge(Discount.is_enabled)(但我得再检查一遍)
发布于 2014-02-28 10:56:30
在折扣模型中,
scope :Active_Orders, -> { where(is_enabled: true) }然后,获取TimeDiscount以获得启用折扣,
@Active_TimeDiscounts = Discount.Active_Orders.map{|active_ord| active_ord.TimeDiscount}希望它有帮助:)
https://stackoverflow.com/questions/22087739
复制相似问题