2016-01-21 99 views
0

我有一個非常適合類別和大喊之間的結構。我要做的就是qeuro在POST類型API的調用具有以下參數:在Ruby on Rails中創建一個包含多對多關係的帖子

{ 
    "user_id":"1", 
    "title":"primeito", 
    "desciption":"de_novo", 
    "categories":[{"name":"eletro"},{"name":"domestic"},{"name":"new_category"}], 
    "yell_type":"novo", 
    "price":"10,00", 
    "payment_type":"boleto" 
} 

我的結構如下:

我的模型大喊:

#yell.rb 
class Yell < ActiveRecord::Base 
    belongs_to :user, inverse_of: :yells 
    has_and_belongs_to_many :categories 
end 

模型類別:

#category.rb 
class Category < ActiveRecord::Base 
    has_and_belongs_to_many :yells 
end 

方法克里特控制器大喊:

#yells_controller.rb 
def create 
@yell = Yell.new(yell_params) 

params[:categories].each do |rel| 
    @category = Category.find_by_name(rel[:name]) 
    if @category 
    #only creates the relationship 
    else 
    @yell.categories.build(name: rel[:name]) #creates the relationship and category 
    end 
end 

if @yell.save 
    render json: @yell, status: :created, location: api_yell_path(@yell) 
    else 
    render json: @yell.errors, status: :unprocessable_entity 
    end 
end 
... 
private: 

    def yell_params 
     params.require(:yell).permit(:title, :desciption, :price, :payment_type, :user_id, :yell_type, :categories) 
    end 

所以我創建的表

class CreateCategoriesYellsJoinTable < ActiveRecord::Migration 
    def self.up 
    create_table :categories_yells, :id => false do |t| 
     t.integer :category_id 
     t.integer :yell_id 
    end 

    add_index :categories_yells, [:category_id, :yell_id] 
    end 

    def self.down 
    drop_table :categories_yells 
    end 
end 

我可以讓他創建的類別,但不知道如何創建唯一的關係。 Agluem可以幫助我的評論#只是建立關係?

我需要,因爲該類別名稱是唯一

做此項檢查同樣,如果有人知道的東西更優雅的方式來做到這一點,我接受建議

回答

0

爲了做到這一點,我不得不創建模型加入我的表:

模型category_yell.rb

class CategoriesYell < ActiveRecord::Base 
    belongs_to :category 
    belongs_to :yell 
end 

create我的方法如下:

def create 
    #@yell = Yell.new(yell_params.except(:categories)) 
    @yell = Yell.new({title: params[:title], desciption: params[:desciption], price: params[:price], user_id: params[:user_id], yell_type: params[:yell_type]}) 

    if @yell.save 
    Array(params[:categories]).each do |rel| 

     @category = Category.find_by_name(rel[:name]) 

     if @category 
     @categories_yells = CategoriesYell.new(category_id: @category.id, yell_id: @yell.id) 

     if @categories_yells.save 
      @yell.categories.build(id: @category.id, name: rel[:name])#only creates the relationship 
     else 
      render json: {status: 1, message:"relationship categoy not create", data: @yell.errors}, status: :unprocessable_entity 
     end 

     else 
     @yell.categories.create(name: rel[:name]) #creates the relationship and category 
     end 

    end 

    render json: {status: 0, message:"sucess", data: @yell}, status: :created 
    else 
    render json: {status: -1, message:"error", data: @yell.errors}, status: :unprocessable_entity 
    end 

end 
0

我不太肯定我明白了最後一段,但我認爲你需要一箇中間表來首先加入這兩個模型。

您將需要創建這樣一個表:

class CreateCategoriesAndYells < ActiveRecord::Migration 
    def change 
    create_table :categories_yells, id: false do |t| 
     t.belongs_to :category, index: true 
     t.belongs_to :yell, index: true 
    end 
    end 
end 

那麼你就需要更新您的控制器說這樣的事情:

@yell.categories.build(category_params) 

你會也需要通過類別參數給控制器。

+0

然後,我創建的表,缺少的是@ yell.categories.build(category_params),但我仍然有一個疑問:要調用這個方法,如果有一個類將被創建?如果有的話只創建關係而不創建新關係? –

相關問題