2012-08-24 62 views
0

我無法在使用Rails的對象上使用destroy方法3.2.6 這是我的代碼。 @checkin找到一個對象,我查了一下。無法在Rails中銷燬對象

@checkin = Checkin.where("user_id = ? AND event_id = ?", current_user.id, params[:event_id]) 

    if @checkin.destroy 
     render :json => {:results => @checkin} 
    else 
     render :json => {:errors => @checkin.errors.full_messages}, status: 422 
    end 

我得到的錯誤:

wrong number of arguments (0 for 1) 

UPDATE

這是堆棧跟蹤:

activerecord (3.2.6) lib/active_record/relation.rb:378:in `destroy' 
actionpack (3.2.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action' 
actionpack (3.2.6) lib/abstract_controller/base.rb:167:in `process_action' 
actionpack (3.2.6) lib/action_controller/metal/rendering.rb:10:in `process_action' 
actionpack (3.2.6) lib/abstract_controller/callbacks.rb:18:in `block in process_action' 
activesupport (3.2.6) lib/active_support/callbacks.rb:414:in `_run__2516945162825430073__process_action__3384959627746267634__callbacks' 
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback' 
activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks' 
activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks' 
actionpack (3.2.6) lib/abstract_controller/callbacks.rb:17:in `process_action' 
actionpack (3.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action' 
actionpack (3.2.6) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action' 
activesupport (3.2.6) lib/active_support/notifications.rb:123:in `block in instrument' 

更新2(在控制檯試過)

@checkin = Checkin.where("user_id = ? AND event_id = ?", 46, 1) 
    Checkin Load (0.3ms) SELECT "checkins".* FROM "checkins" WHERE (user_id = 46 AND event_id = 1) 
=> [#<Checkin id: 1, user_id: 46, event_id: 1, created_at: "2012-08-15 14:14:56", updated_at: "2012-08-15 18:18:40">] 

ruby-1.9.2-p290 :009 > @checkin.destroy 
ArgumentError: wrong number of arguments (0 for 1) 
    from /Users/dkrusenstrahle/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.6/lib/active_record/relation.rb:378:in `destroy' 
    from (irb):9 
    from /Users/dkrusenstrahle/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start' 
    from /Users/dkrusenstrahle/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start' 
    from /Users/dkrusenstrahle/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>' 
    from script/rails:6:in `require' 
    from script/rails:6:in `<main>' 
+0

你可以發佈完整的堆棧跟蹤嗎? – zsquare

+0

更新了我的帖子。 –

回答

3

where始終爲您提供了陣列和陣列on't有破壞方法,主動記錄對象有破壞的方法,破壞這個

@checkin = Checkin.where("user_id = ? AND event_id = ?", 46, 1).first 
@checkin.destroy if @checkin 
2

因爲@checkin是一個集合,你應該使用,

@checkin.destroy_all