Rails 3 在 has_many 关系中合并相似的实例

发布于 2024-11-09 08:17:33 字数 924 浏览 0 评论 0原文

我有两个型号。订单和行项目。

class Order < ActiveRecord::Base  
    has_many :line_items
    has_many :products, :through => :line_items
end

class LineItem < ActiveRecord::Base
    belongs_to :order
    belongs_to :product
end

line_items 包含以下属性 - :product_id:size:quantity

对于相同的产品和尺寸,一个订单可能有多个 line_item,在这种情况下,我希望能够访问合并为一个的 line_item,其数量为所有 line_item 的总数量。例如,订单可能具有以下 line_items:

# | product_id | size   | quantity
1 | 1          | small  | 1
2 | 1          | small  | 2
3 | 1          | medium | 1
4 | 2          | small  | 1

应该合并到:

# | product_id | size   | quantity
1 | 1          | small  | 3
2 | 1          | medium | 1
3 | 2          | small  | 1

对我来说,这应该是订单模型上的范围或类方法似乎很自然,因此我可以调用 @order.line_items_merged< /code> 或类似的视图,但我不太确定如何实现它。有人可以帮忙吗?

I have two models. order and line_item.

class Order < ActiveRecord::Base  
    has_many :line_items
    has_many :products, :through => :line_items
end

class LineItem < ActiveRecord::Base
    belongs_to :order
    belongs_to :product
end

line_items include the following attributes - :product_id, :size, :quantity.

It's possible that an order can have more than one line_item for the same product and size, in which case, I want to be able to access the line_items merged into one with the quantity being the total of all of them. For example, an order might have the following line_items:

# | product_id | size   | quantity
1 | 1          | small  | 1
2 | 1          | small  | 2
3 | 1          | medium | 1
4 | 2          | small  | 1

Which should be merged to:

# | product_id | size   | quantity
1 | 1          | small  | 3
2 | 1          | medium | 1
3 | 2          | small  | 1

It seems natural to me that this should either be a scope or a class method on the order model, so I can call @order.line_items_merged or something similar from the view, but I'm not quite sure how to implement it. Can anyone help?

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

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

发布评论

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

评论(2

巾帼英雄 2024-11-16 08:17:33

同一对(产品、尺寸)拥有多个订单项真的有意义吗?您实际上会单独使用它们还是仅合并使用它们?

就我个人而言,我会考虑更改逻辑,以便您不能为相同的产品和尺寸拥有多个行项目(即直接增加数量,而不是创建多个记录来合并),从而避免问题!

Does it really make sense to have multiple line items for the same (product, size) pair? Will you actually ever use them separately, or only merged?

Personally I would consider changing the logic so that you can't have more than one line item for the same product and size (i.e. incrementing quantity directly, rather creating multiple records to merge), and thus avoid the issue!

三寸金莲 2024-11-16 08:17:33

好的,那么为什么要将 line_items 分组到单个虚拟实体中呢?

对我来说,在 Order 类中创建方法/范围来迭代 line_items 并获取需要的数据似乎更容易。再次不确定您的最终目标是什么。

Order 类:

def qty
  line_items.map{|li| li.quantity}.sum
end

def sizes
  line_items.map{|li| li.size}.uniq
end

如果订单已加载到内存中,则非常简单的示例 - 希望有所帮助。
如果需要数据库搜索/排序,那么您可以在 lib 目录中定义一个 line_items 模块,并直接从复数 line_items 调用中调用方法...对于“Orders”也可以这样做,

所以 扩展你的模型关联声明:

has_many :line_items, :extend => LineItemsMethods, :dependent => :destroy

LineItemsMethods类:

def any_special_products?
  any? {|line_item| line_item.special_method}   
end

范围当然也可以工作......试图保持简单,希望这有帮助。


编辑

我完全错过了示例的第二行,其中一个 line_item 的数量已经为 2。所以我同意第一个答案:如果您要让用户输入数量,则可以选择再次使用相同的产品;它应该只更新现有数量,无论它是否已保存...

Ok so why do you want to group the line_items into a single virtual entity?

Seems easier to me to just created methods / scopes in the Order class that iterates through line_items and fetches the data needed. Again not sure what your end goal is here.

Order class:

def qty
  line_items.map{|li| li.quantity}.sum
end

def sizes
  line_items.map{|li| li.size}.uniq
end

Very simple examples if the order is loaded into memory already- hope that helps.
If DB searching / sorting is required then you could define a line_items module say in the lib directory and call methods directly off of the plural line_items calls... same could be done for 'Orders'

So extend your model association declaration:

has_many :line_items, :extend => LineItemsMethods, :dependent => :destroy

LineItemsMethods class:

def any_special_products?
  any? {|line_item| line_item.special_method}   
end

Scopes would work too of course... tried to keep it simple, hope thats helps.


Edit

I totally missed line two of your example where one line_item already had a quantity of 2. So I agree with the first answer: if you are going to have the user enter a quantity then be able to select that same product again; it should just update the existing quantity regardless if it is saved yet or not...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文