Rails 3 在 has_many 关系中合并相似的实例
我有两个型号。订单和行项目。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
同一对(产品、尺寸)拥有多个订单项真的有意义吗?您实际上会单独使用它们还是仅合并使用它们?
就我个人而言,我会考虑更改逻辑,以便您不能为相同的产品和尺寸拥有多个行项目(即直接增加数量,而不是创建多个记录来合并),从而避免问题!
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!
好的,那么为什么要将
line_items
分组到单个虚拟实体中呢?对我来说,在
Order
类中创建方法/范围来迭代line_items
并获取需要的数据似乎更容易。再次不确定您的最终目标是什么。Order
类:如果订单已加载到内存中,则非常简单的示例 - 希望有所帮助。
如果需要数据库搜索/排序,那么您可以在 lib 目录中定义一个 line_items 模块,并直接从复数
line_items
调用中调用方法...对于“Orders”也可以这样做,所以
扩展
你的模型关联声明:LineItemsMethods
类:范围当然也可以工作......试图保持简单,希望这有帮助。
编辑
我完全错过了示例的第二行,其中一个
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 throughline_items
and fetches the data needed. Again not sure what your end goal is here.Order
class: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:LineItemsMethods
class: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...