我不能在使用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>'
答案 0 :(得分:3)
嗨where
总是给你数组和数组都有破坏方法,活动记录对象有破坏方法,要破坏这个
@checkin = Checkin.where("user_id = ? AND event_id = ?", 46, 1).first
@checkin.destroy if @checkin
答案 1 :(得分:2)
由于@checkin
是一个集合,您应该使用
@checkin.destroy_all