【发布时间】:2016-04-27 19:35:24
【问题描述】:
如何显示分为三个部分的产品(三个不同的过滤器,例如:product_1、product_2、product_3),并且只需要从每个部分中选择一个产品
提交后。我应该为一个订单保存所有这些产品。
我有 4 张桌子: 用户 订单 订单详细信息 产品
class Order < ActiveRecord::Base
has_many :order_details
belongs_to :user
has_many :products, through: :order_details
accepts_nested_attributes_for :order_details
end
class OrderDetail < ActiveRecord::Base
belongs_to :order
belongs_to :product
end
class Product < ActiveRecord::Base
has_many :order_details
has_many :orders, through: :order_details
def self.get_first_course
Product.where(product_type: "exem_product_1")
end
def self.get_main_course
Product.where(product_type: "exem_product_2")
end
def self.get_drink
Product.where(product_type: "exem_product_3")
end
end
我不确定如何为这种情况编写强大的参数以及如何创建对象以保存数据。
class OrdersController < ApplicationController
before_action :authenticate_user!
def index
@order = Order.new
#I think need something like this..?!
#@order.order_details.build
end
def create
end
private
def order_params
params.require(:order).permit(:date, :product_id => [])
end
end
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 simple-form has-many-through fields-for