0

我已經設置has_many和has_many:通過Order,User,Product和Order_detail模型之間的關聯作爲連接表。Rails - has_many:通過關聯+保存關聯數據

型號:

class Order < ActiveRecord::Base 
    has_many :order_details 
    belongs_to :user 
    has_many :products, through: :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 
end 

class User < ActiveRecord::Base 
    has_many :orders 
end 

如何自動連接表ORDER_DETAILS保存。 現在數據只保存到訂單表中。 需要保存所有產品order_tables爲當前的訂單和用戶

class OrdersController < ApplicationController 
    before_action :authenticate_user! 

    def index 
    @order = Order.all 
    end 

    def new 
    # @order = Order.new 
    @order = current_user.orders.new 
    end 

    def create 
    @order = current_user.orders.new(order_params) 
    @order.date = Date.today.to_s 
    if @order.save 
     # i think this is bad wrong to implementation of functional) 
     # params[:product_id].each do |detail_product_id| 
     # @order.order_details.product_id = detail_product_id 
     # @order.order_details.user_id = current_user 
     # @order.order_details.save 
     flash[:success] = "Order was successfully submitted" 
     redirect_to new_order_path 
    else 
     render :new 
    end 
    end 

private 

def order_params 
    params.require(:order).permit(:date, :product_id => []) 
end 
end 

我的架構:

create_table "order_details", force: true do |t| 
    t.integer "order_id" 
    t.integer "product_id" 
    t.integer "quantity" 
    t.integer "price" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
end 

create_table "orders", force: true do |t| 
t.integer "user_id" 
t.date "date" 
end 

add_index "orders", ["user_id"], name: "index_orders_on_user_id", using: :btree 

create_table "orders_products", id: false, force: true do |t| 
    t.integer "order_id" 
    t.integer "product_id" 
end 

create_table "products", force: true do |t| 
    t.string "product_name" 
    t.integer "product_price" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    t.boolean "available_status" 
    t.string "product_type" 
end 
+1

您可能需要[nested_attributes(http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods。html)爲這個 –

回答

-1

更改我的聯想類型的habtm和這足以讓我的處境。所以..

型號:

class Order < ActiveRecord::Base 
    has_and_belongs_to_many :products 
    before_destroy { products.clear } 
    end 
    class Product < ActiveRecord::Base 
    has_and_belongs_to_many :orders 
    end 

Order_controller:

def create 
    order = current_user.orders.new(date: Date.today.to_s) 
    @order_products = Product.where(id: order_params[:product_ids]) 
    order.products << @order_products 
    if order.save 
    #blalblal - sucsess 
    else 
    #blabla - alert-notice 
    end 
1

在您看來,加個領域像ORDER_DETAILS:

 <%= f.fields_for :order_details do |od| %> 
     <%= od.label 'your attribute for OrderDetail' %> 
     <%= # od.text_field 'your attribute' %> 
    <% end %> 

然後在你的模型,接受order_details的嵌套屬性 像:

accepts_nested_attributes_for :order_details 

這些是樣本值,您可以將此邏輯與您的實際屬性一起使用。

在你的控制,允許像ORDER_DETAILS屬性:

params.require(:order).permit(:id, :name, order_details: [ 
     #attributes of order details 
    ]) 
1

假設product_ids是要添加到訂單的產品ID的數組,你可以嘗試下面的方式來進行分配和Rails的應該會自動創建ORDER_DETAILS這些協會的記錄,當你再調用@ order.save

@order.products << Product.find_by_id(product_ids) 
+0

thk!我編輯了一些東西,需要建議 –

0

我說得對補充的是,控制器行? Order_controller:

def create 
    @order = current_user.orders.new(order_params) 
    @order.products = Product.find_by_id(order_params[:product_ids]) 
    @order.date = Date.today.to_s 
    if @order.save 
     flash[:success] = "Order was successfully submitted" 
     redirect_to new_order_path 
    else 
     render :new 
    end 
    end 

private 
def order_params 
    params.require(:order).permit(:date, order_details: [:product_id]) 
end 
end 

訂貨型號:

accepts_nested_attributes_for :order_details 

我想從3種不同類型的產品保存。

product_details

  • 但如何把一個產品ID從每個零件?因爲現在我可以選擇從所有

僅查看一個產品:

= simple_form_for(@order, html: {:class => 'well form-horizontal', :method => :post, :action=> :create }) do |f| 
    .col-xs-12.col-sm-6.col-md-8 
    = render 'shared/error_messages', object: f.object 
    %br 
    = simple_fields_for :order_details do |od| 
     = od.collection_radio_buttons :product_ids, Product.get_first_course, :id, :product_name ,:item_wrapper_class => 'inline' 
     %hr 
     = od.collection_radio_buttons :product_ids, Product.get_main_course, :id, :product_name, :item_wrapper_class => 'inline' 
     %hr 
     = od.collection_radio_buttons :product_ids, Product.get_drink, :id, :product_name,:item_wrapper_class => 'inline' 
    %hr 
    = f.button :submit, class: "btn btn-primary" 
+1

我無法幫助simple_form的東西,但一看就知道控制器應該稍微改變一點。 order.products = Product.find_by_id(order_params [:product_ids])不會工作,因爲@ order.products是一個集合。更改=爲<< – hypern

+1

同樣在您的強參數中,您可能需要將.permit(:date,order_details:[:product_id])更改爲.permit(:date,order_details:[product_ids:[]]) – hypern

+0

push和< <相同(相等)的東西? order.products.push Product.find_by_id(order_params [:product_ids])order.products << Product.find_by_id(order_params [:product_ids]) –

相關問題