2016-04-05 14 views
1

私のIngredientモデルの "name"属性を検証しようとしています。しかし、自分のIngredientモデルでvalidates:name、:uniqueness => trueを追加すると、名前を検証しません(同じ名前の成分を挿入できます)。成分モデルは他のモデルと複雑な関係にあります。以下のコードをご覧ください。Rails 4-複合モデル検証(has_many、through)

私の究極の目標は、成分が成分表に存在しない場合に成分を作成することです。成分がすでに存在している場合は、成分idを量として使用します。誰かがそれを達成する方法を知っていれば、解決策を提供してください。前もって感謝します。

%strong Ingredients: 
    %fieldset#recipe-ingredients 
    %br 
    = f.fields_for :quantities do |builder| 
     = render 'recipe/quantity_fields', f: builder 
    .links 
     = link_to_add_association 'add ingredient', f, :quantities, 'data-association-insertion-node' => '#recipe-ingredients', 'data-assoication-insertion-moethod' => "append", :wrap_object => Proc.new{|quantity| quantity.ingredient.build ; quantity} 
      %br 
:成分を作成するため

class Ingredient < ActiveRecord::Base 
    has_many :quantities 
    has_many :recipes, through: :quantities 

    validates :name, :presence => true, :uniqueness => true 
end 

quantity.rb

class Quantity < ActiveRecord::Base 
    belongs_to :ingredient 
    belongs_to :recipe 

    accepts_nested_attributes_for :ingredient, 
           :reject_if => :all_blank 

    validates :ingredient, :uniqueness => true 
end 

recipe.rb

class Recipe < ActiveRecord::Base 
    has_many :quantities, 
       dependent: :destroy 
    has_many :ingredients, 


       :through => :quantities 

     accepts_nested_attributes_for :quantities, 
             reject_if: :all_blank, 
             allow_destroy: true 

     accepts_nested_attributes_for :ingredients 
end 

ビュー部ingredient.rb

成分コントローラ:

class IngredientController < ApplicationController 
    before_action :set_ingredient, only: [:show, :edit, :update, :destroy] 

    # GET /ingredients 
    # GET /ingredients.json 
    def index 
    @ingredients = Ingredient.all 
    end 

    # GET /ingredients/1 
    # GET /ingredients/1.json 
    def show 
    end 

    # GET /ingredients/new 
    def new 
    @ingredient = Ingredient.new 
    end 

    # GET /ingredients/1/edit 
    def edit 
    end 

    def create 
    @ingredient = Ingredient.new(ingredient_params) 

    respond_to do |format| 
     if @ingredient.save 
     format.html { redirect_to @ingredient, notice: 'Ingredient was successfully created.' } 
     format.json { render :show, status: :created, location: @ingredient } 
     else 
     format.html { render :new } 
     format.json { render json: @ingredient.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /ingredients/1 
    # PATCH/PUT /ingredients/1.json 
    def update 
    respond_to do |format| 
     if @ingredient.update(ingredient_params) 
     format.html { redirect_to @ingredient, notice: 'Ingredient was successfully updated.' } 
     format.json { render :show, status: :ok, location: @ingredient } 
     else 
     format.html { render :edit } 
     format.json { render json: @ingredient.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /ingredients/1 
    # DELETE /ingredients/1.json 
    def destroy 
    @ingredient.destroy 
    respond_to do |format| 
     format.html { redirect_to ingredients_url, notice: 'Ingredient was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_ingredient 
     @ingredient = Ingredient.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def ingredient_params 
     params.require(:ingredient).permit(:name) 
    end 
end 
+0

どのように検証されませんか?重複したエントリを取得していますか? –

+0

重複したエントリ。 (例:同じ名前の成分を複数回挿入することができます) – SL07

+0

同じ場合ですか?確かに必要はありません:一意性=> {:case_sensitive => false}? –

答えて

関連する問題