当子对象满足特定条件时更新父对象

时间:2015-11-01 13:52:31

标签: ruby-on-rails

我有2个模型 - 订单和项目:

order.rb:

class Order < ActiveRecord::Base
  has_many :items
end

item.rb的:

class Item < ActiveRecord::Base
belongs_to :order
end
项目的

架构:

t.decimal  "price",       precision: 12, scale: 3
t.string   "status"

订单架构:

 t.string   "status"

当用户收到商品时,商品的状态将被标记为已发货。那么如何在所有项目的条件下将我的订单状态更新为&#34;完成&#34;状态更新为&#34;已发货&#34;?

2 个答案:

答案 0 :(得分:0)

我会选择这样的事情:

www.example.com/view

每次class Item < ActiveRecord::Base def complete_order order.complete if all_items_shipped? end def shipped? status == 'shipped' end private def all_items_shipped? order.items.all?(&:shipped?) end end class Order < ActiveRecord::Base def complete update status: 'complete' end end 被标记为item.complete_order时,请致电Item

答案 1 :(得分:0)

您需要更新items表格以包含foreign key,并将其与orders相关联:

$ rails g new migration AddOrderIDToItems

#db/migrate/add_order_id_to_items______.rb
class AddOrderIdToItems < ActiveRecord::Migration
   def change
      add_column :items, :order_id, :integer
   end
end

$ rake db:migrate

您可以详细了解这一重要原因here

enter image description here

-

这将允许您执行以下操作:

#app/models/order.rb
class Order < ActiveRecord::Base
   has_many :items, inverse_of: :order
end

#app/models/item.rb
class Item < ActiveRecord::Base
   belongs_to :order, inverse_of: :items
   after_save :check_order, on: :update

   private

   def check_order
      items = Item.where(order_id: order.id).where.not(status: "shipped").count
      order.update(status: "complete") if items > 0
   end
end

这将允许您使用以下内容:

@item = Item.find params[:id]
@item.update(status: "shipped") #-> "check_order" will happen, saving "order" as "complete" if all items are shipped

真正的解决方法是创建has_many :through关系,将多个Items与多个Orders相关联。这样,您就可以将每个OrderItems标记为&#34;已发货&#34;:

#app/models/order.rb
class Order < ActiveRecord::Base
   has_many :order_items
   has_many :items, through: :order_items
end

#app/models/order_item.rb
class OrderItem < ActiveRecord::Base
   #columns id | order_id | item_id | created_at | updated_at
   belongs_to :order
   belongs_to :item

   after_save :check_order, on: :update

   private

   def check_order
      items = Item.where(order_id: order.id).where.not(status: "shipped").count
      order.update(status: "complete") if items > 0
   end
end

#app/models/item.rb
class Item < ActiveRecord::Base
   has_many :order_items
   has_many :orders, through: :order_items
end

您只需要介绍OrderItem模型(不需要更改为ItemOrder模型):

enter image description here

这将允许您执行以下操作:

@order = Order.find params[:id]
@item = Item.find params[:id]

@order.items << @item #-> adds "item" to order 

然后您可以使用以下内容来设置订单是否已经完成&#34;与否:

@order = Order.find params[:id]

@item = @order.order_items.find x
@item.update status: "shipped"