2016-12-16 73 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 

一个收购对象我知道错误是因为某种关联的我有,但我似乎无法弄清楚。我的模型如下

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 

数据库模式:

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。这不是你的关联问题。

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

我使用.find_by代替。哪里的,因为。凡返回多个匹配。 .find_by总是返回它找到的第一个。

如果你确实要销毁所有比赛,你可以使用。凡和.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

很高兴听到。你能标记我的答案吗? –

相关问题