Ruby on Rails 3 - 连接多个模型,使用另一个不直接相关的模型的属性来更新一个模型的字段
我在尝试使用另一个模型的值更新一个模型的属性时遇到问题,但这两个模型不相关。
我的模型是:
class Order < ActiveRecord::Base
has_many :order_details
has_many :waybills
end
class OrderDetail < ActiveRecord::Base
belongs_to :order, :foreign_key => "order_id"
belongs_to :product, :foreign_key => "product_id"
end
class Waybill < ActiveRecord::Base
has_many :purchases
belongs_to :order, :foreign_key => 'order_id'
end
class Purchase < ActiveRecord::Base
belongs_to :waybill, :foreign_key => 'waybill_id'
has_many :detail_purchases
end
class DetailPurchase < ActiveRecord::Base
belongs_to :purchase, :foreign_key => 'purchase_id'
belongs_to :product, :foreign_key => 'product_id'
end
因此,正如您所看到的......“DetailPurchase”属于“Order”,但是以间接的方式。并且“OrderDetail”直接属于“Order”。
我需要使用“DetailPurchase”产品的属性“数量”更新“OrderDetail”产品的属性“数量”。
这个想法是“新”OrderDetail.quantity =“旧”OrderDeatil.quantity - DetailPurchase.quantity(显然product_id必须相同)
所以,我不知道如何使用“rails”“编写”查询way”(可能是“Model.find()”、“Model.where()”、“Model.update_attribute()”)或仅使用原始 sql 查询(顺便说一句,我不知道如何编写或执行原始 SQL 查询)
您有什么建议?谢谢
I have a problem trying to update an attribute of one model using the value from another model, but those two models are not related.
My models are:
class Order < ActiveRecord::Base
has_many :order_details
has_many :waybills
end
class OrderDetail < ActiveRecord::Base
belongs_to :order, :foreign_key => "order_id"
belongs_to :product, :foreign_key => "product_id"
end
class Waybill < ActiveRecord::Base
has_many :purchases
belongs_to :order, :foreign_key => 'order_id'
end
class Purchase < ActiveRecord::Base
belongs_to :waybill, :foreign_key => 'waybill_id'
has_many :detail_purchases
end
class DetailPurchase < ActiveRecord::Base
belongs_to :purchase, :foreign_key => 'purchase_id'
belongs_to :product, :foreign_key => 'product_id'
end
So, as you can see... a "DetailPurchase" belongs to an "Order", but in an indirect way. And an "OrderDetail" belongs to an "Order" directly.
I need to update the attribute "quantity" of the product of "OrderDetail" using the attribute "quantity" of the product of "DetailPurchase".
The idea is the "new" OrderDetail.quantity = "old" OrderDeatil.quantity - DetailPurchase.quantity (and obviously the product_id has to be the same)
So, i don't know how to "write" the query using the "rails way" (could be "Model.find()", "Model.where()", "Model.update_attribute()") or just use a raw sql query (by the way, I don't know how to write or execute raw sql queries)
What do you recommend? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解您的问题,那么您正在尝试查找给定 DetailPurchase 的(松散)关联的 OrderDetail。为此,您可以这样做:
话虽如此,您可能需要更改一些事情:
首先也是最重要的,您几乎肯定不应该有一个名为“order”的模型 - order 也是一个 ActiveRecord 实例方法(就像
ClassName.all.order("name ASC")
) 一样,所以你正在向一个充满伤害的世界敞开自己。让一个模型在另一个模型发生变化时更新另一个模型中的聚合值似乎也有点问题;在编辑或删除 OrderDetails 时,您将需要处理所有各种棘手的逻辑。对 OrderDetail.quantity 使用聚合函数可能更有意义 - 换句话说,在创建时设置 OrderDetail.original_quantity,然后将 OrderDetail.quantity 计算为
OrderDetail.original_quantity 减去相关购买详细信息数量的总和.如果您想走这条路,我会忽略上面的代码并将purchase_details作为作用域方法添加到OrderDetail;有关详细信息,请参阅API。
If I understand your question, you're trying to find the (loosely) associated OrderDetail for a given DetailPurchase. To do this, you can do:
That being said, there are a few things you might want to change:
First and foremost, you almost certainly shouldn't have a model named 'order' -- order is also an ActiveRecord instance method (as in
ClassName.all.order("name ASC")
), so you're opening yourself up to a world of hurt.It also seems a bit problematic to be having one of your models updating an aggregate value in another model when it changes; you're going to have all sorts of tricky logic to deal with when editing or deleting OrderDetails. It might make more sense to use an aggregate function for OrderDetail.quantity -- in other words, set OrderDetail.original_quantity on create, then calculate OrderDetail.quantity as
OrderDetail.original_quantity minus the sum of the relevant purchase detail quantities
. If you want to go this route, I'd ignore the code above and add purchase_details as a scope method to OrderDetail; see the API for details.