0

我不能在使用 Rails 3.2.6 的对象上使用 destroy 方法 这是我的代码。@checkin 找到一个对象,我检查了。

@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)

更新

这是堆栈跟踪:

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>'
4

2 回答 2

3

where总是给你数组和数组没有销毁方法,活动记录对象有销毁方法,销毁这个

@checkin = Checkin.where("user_id = ? AND event_id = ?", 46, 1).first
@checkin.destroy if @checkin
于 2012-08-24T11:56:06.660 回答
2

因为@checkin是一个集合,你应该使用,

@checkin.destroy_all
于 2012-08-24T12:50:11.433 回答