2015-10-05 124 views
2

我在項目中有一個多重has_many關係,並且正在尋找如何使用表單更新值的幫助。相同模型的不同實例(通過關聯多個has_many)

有兩種基本模式:產品和國家。產品可以來自多個來源國家,並有多個目的地國家。 (我不能拿出一個很好的比喻爲例子)

create_table "nations", force: :cascade do |t| 
    t.string "name" 
end 

create_table "products", force: :cascade do |t| 
    t.string "name" 
    t.string "bio" 
end 

create_table "dest_nation", force: :cascade do |t| 
    t.integer "product_id" 
    t.integer "nation_id" 
end 

create_table "orig_nation", force: :cascade do |t| 
    t.integer "product_id" 
    t.integer "nation_id" 
end 

然後我創建的關係如下:

class Product < ActiveRecord::Base 
    has_many :dest_nations 
    has_many :destination, :class_name=> 'nations', through: :dest_nation 
    has_many :orig_nations 
    has_many :origin, :class_name=> 'nations', through: :orig_nations 
end 

class Nation < ActiveRecord::Base 
    has_many :dest_nations 
    has_many :imports, :class_name => 'products', through: :dest_nation 
    has_many :orig_nations 
    has_many :exports, :class_name =>'products', through: :orig_nations 
end 

class DestNation < ActiveRecord::Base 
    belongs_to :nation 
    belongs_to :product 
end 

class OrigNation < ActiveRecord::Base 
    belongs_to :nation 
    belongs_to :product 
end 

問題: 1)當產品被註冊,我想了解用戶可以從選項列表中選擇的源和目標國家。我如何在視圖和控制器中進行這些更改以適當反映這些更改。

<%= form_for @product do |f|%> 
    <div class="form-group"> 
    <%= f.label :name, "Name of the product:"%> 
    <%= f.text_field :name, class: "form-control" %> 
    </div> 
    <div class="form-group"> 
    <%= f.label :bio, "A brief summary:"%> 
    <%= f.text_area :bio, class: "form-control", rows:3%> 
    </div> 
    <!-- For origination country --> 
    <%= f.collection_check_boxes :nation_ids, Nation.all, :id, :name do |cb|%> 
    <% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") + cb.text}%> 
    <% end %> 
    <br> 
<!-- For destination country --> 
    <%= f.collection_check_boxes :nation_ids, Nation.all, :id, :name do |cb1|%> 
    <% cb1.label(class: "checkbox-inline input_checkbox") {cb1.check_box(class: "checkbox") + cb1.text}%> 
    <% end %> 
    <br>     
<%= f.submit "Submit", class: "btn btn-default btn-primary" %> 
<%end%> 

我的產品控制器

def create 
    @product = Product.new(product_params) 
    if @product.save 
    flash[:success] = "" 
    redirect_to root_path 
    else 
    render 'new' 
    end  
    # binding pry 
end 

def product_params 
    params.require(:product).permit(:name,:bio, nation_ids: []) 
end 
+0

你有沒有想過使用simple_form寶石更新? https://github.com/plataformatec/simple_form。它會爲你提供協會助手,然後你可以使用,例如:'f.association:destinations' – IngoAlbers

回答

0

終於想出瞭解決辦法。

只上市的編輯回答部分在這裏

模型/ product.rb

class Product < ActiveRecord::Base 
    has_many :dest_nations 
    has_many :destinations, through: :dest_nations, :source => 'nation' 
    has_many :orig_nations 
    has_many :origins, through: :orig_nations, :source => 'nation' 
end 

做過類似更改nation.rb

的表單代碼讀取(new.html。ERB)

<%= f.collection_check_boxes :origin_ids, Nation.all, :id, :name do |cb1|%> 
<% cb1.label(class: "checkbox-inline input_checkbox") {cb1.check_box(class: "checkbox") + cb1.text}%> 

最後的產品params爲如下(在products_controller.rb)

params.require(:product).permit(:name,:bio, origin_ids: [], destination_ids: []) 
0

產品可以從多個原產國進行採購,並有多個 目的地國家

聽起來你正在尋找has_and_belongs_to_many協會:

enter image description here

這會讓你的問題更簡單處理:

#app/models/product.rb 
class Product < ActiveRecord::Base 
    has_and_belongs_to_many :destinations, 
     :class_name =>"Nation", 
     join_table: :destinations, 
     foreign_key: :product_id, 
     association_foreign_key: :nation_id 

    has_and_belongs_to_many :origins, 
     :class_name =>"Nation", 
     join_table: :origins, 
     foreign_key: :product_id, 
     association_foreign_key: :nation_id 
end 

#app/models/nation.rb 
class Nation < ActiveRecord::Base 
    has_and_belongs_to_many :exports, 
     :class_name=> "Product", 
     join_table: :origins, 
     foreign_key: :nation_id, 
     association_foreign_key: :product_id 

    has_and_belongs_to_many :imports, 
     :class_name=> "Product", 
     join_table: :destinations, 
     foreign_key: :nation_id, 
     association_foreign_key: :product_id 
end 

你必須用下面的表格來收回這些:

create_table "destinations", id: false do |t| 
    t.integer "product_id" 
    t.integer "nation_id" 
end 

create_table "origins", id:false do |t| 
    t.integer "product_id" 
    t.integer "nation_id" 
end 

如何我可以在視圖和控制器中將這些變化適當地反映到

#app/controllers/products_controller.rb 
class ProductsController < ApplicationController 
    def new 
     @product = Product.new 
    end 

    def create 
     @product = Product.new(product_params) 
    end 

    private 

    def product_params 
     params.require(:product).permit(:x, :y, :origins, :destinations) 
    end 
end 

#app/views/products/new.html.erb 
<%= form_for @product do |f|%> 
    <%= f.collection_check_boxes :origins, Nation.all, :id, :name %> 
    <%= f.submit %> 
<% end %> 
+0

謝謝,我會試試這個,但是你能否爲has_many提供一個解決方案。我想操作干預模型,這對於has_many_and_belongs_to來說是不可能的。 –

相關問題