我有一个表格,其中行中的产品和列中的供应商。用户可以输入每个产品/供应商组合的订单数量。
为每个供应商创建 1 个订单。每个订单都包含 OrderItems。为用户输入数量的每个字段(产品/供应商组合)创建 OrderItems。
有很多手写代码来处理提交的表单。
有没有办法干掉下面的任何代码?还是有更好的方法?
我检查了嵌套表单 Railscast,但我看不出如何在这里使用 Accept_nested_attributes_for,因为输入是二维的(供应商和产品的组合)。
class Product < ActiveRecord::Base
has_many :order_items
end
class Supplier < ActiveRecord::Base
has_many :orders
end
# groups all OrderItems for 1 Supplier
class Order < ActiveRecord::Base
has_many :order_items
belongs_to :supplier
def self.create_orders_and_order_items(orders)
orders.each do |supplier_id, order_items|
if order_has_order_item?(order_items)
order = create!(
:total => 0,
:supplier_id => supplier_id,
:order_group_id => order_group.id
)
OrderItem.create_order_items(order, order_items)
# update attributes
order.update(:total => order.order_items.sum(:total))
end
end
end
def self.order_has_order_item?(order_items)
sum = 0
order_items.each do |product_id, quantity|
sum += quantity.to_i
end
sum > 0 ? true : false
end
end
# 1 OrderItem per product / supplier combination
class OrderItem < ActiveRecord::Base
belongs_to :order
belongs_to :supplier
belongs_to :product
def self.create_order_items(order, order_items)
order_items.each do |product_id, quantity|
if quantity.to_i > 0
order_item = create!(
:quantity => quantity,
:product_id => product_id,
:order_id => order.id,
)
# update after creating, because methods called below are only available once object has been instantiated
order_item.udpate(:total => order_item.calculate_total)
end
end
end
end
class OrdersController < ApplicationController
def create
Order.create_orders_and_order_items(params[:orders])
respond_to do |format|
format.html { redirect_to :action => "index" }
end
end
end
# view: _form.html.erb
<table>
<tr>
<td>Name</td>
<% @suppliers.each do |supplier| %>
<td COLSPAN=2><%= supplier.name %></td>
<% end %>
</tr>
<% @products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%#= some price %></td>
<td><%= f.text_field "#{supplier.id}[#{product.id}]", :value => "" %></td>
</tr>
</table>
<%= f.submit %>
# params (from memory)
{"orders" => {
"4" => # supplier_id, 1 Order for each Supplier
{ "13" => "2" } # product_id => quantity, = 1 OrderItem
}
}