我有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;?
答案 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:
-
这将允许您执行以下操作:
#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
模型(不需要更改为Item
和Order
模型):
这将允许您执行以下操作:
@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"