2016-12-16 7 views
0

私は、エラーが原因で関連のいくつかの並べ替えである知っているこのアクティブレコードと、ArgumentError破壊(1を与えられた引数(の間違った番号を、)0を予想):

def destroy 
     @purchase=current_user.purchases.where(flooding_property_id: params[:id]) 
     @purchase.destroy(flooding_property_id: params[:id]) 
    end 

などの購入のオブジェクトを破壊しようとしていますI持っているが、私はそれを把握していないようだ。

create_table "flooding_properties", force: :cascade do |t| 
    t.string "address" 
    t.string "zipcode" 
    t.geography "latlon",  limit: {:srid=>4326, :type=>"point", :geographic=>true} 
    t.datetime "last_updated" 
    t.datetime "created_at",               null: false 
    t.datetime "updated_at",               null: false 
    end 

    add_index "flooding_properties", ["latlon"], name: "index_flooding_properties_on_latlon", using: :gist 
    add_index "flooding_properties", ["zipcode"], name: "index_flooding_properties_on_zipcode", using: :btree 

    create_table "purchases", force: :cascade do |t| 
    t.boolean "billed",    default: false 
    t.integer "user_id" 
    t.integer "flooding_property_id" 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    end 

    add_index "purchases", ["billed"], name: "index_purchases_on_billed", where: "(billed = false)", using: :btree 
    add_index "purchases", ["flooding_property_id"], name: "index_purchases_on_flooding_property_id", using: :btree 
    add_index "purchases", ["user_id"], name: "index_purchases_on_user_id", using: :btree 

    create_table "users", force: :cascade do |t| 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    t.string "first_name" 
    t.string "last_name" 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    end 

答えて

1

あなたは、単一のインスタンスで#destroyを呼び出す:

class FloodingProperty < ActiveRecord::Base 
    has_many :purchases 
    has_many :users, through: :purchases 
end 

購入モデル

class Purchase < ActiveRecord::Base 

    belongs_to :user 
    belongs_to :flooding_property 
end 

ユーザーモデル

class User < ActiveRecord::Base 

    has_many :purchases 
    has_many :carts 
    has_many :flooding_properties, through: :purchases 
end 

データベーススキーマは次のように私のモデルがあります。これはあなたの団体の問題ではありません。

def destroy 
    current_user 
    .purchases 
    .find_by(flooding_property_id: params[:id]) 
    .destroy 
end 

.whereでは.whereの代わりに.find_byを使用しています。これは、複数の一致が返されるためです。 .find_byは常に最初に見つかったものを返します。

あなたは確かにあなたが.whereと.destroy_all使用できるすべての一致を破壊したい場合:

def destroy 
    current_user 
    .purchases 
    .where(flooding_property_id: params[:id]) 
    .destroy_all 
end 
+0

うわー、それを解決します。どうもありがとうございます!もともとは問題だと思っていましたが、@ purchase.first.destroy(flooding_property_id:params [:id])を試しましたが、うまくいきませんでした。 – beewuu

+0

これは素晴らしいことです。私の答えに印を付けることができますか? –

関連する問題