1
I 오류를 받고 있어요

: 나는 모든 작업 상태를 업데이트하는 나의 방법을 정의 내 Task 모델에레일 4 update_all 구문 - 인수 오류

Wrong number of arguments (2 for 1)

. 올바른 구문은 무엇입니까?

class Task < ActiveRecord::Base 
    belongs_to :user 

    def self.toggle(user, groups) 
    groups.each do |status, ids| 
     user.tasks.update_all({status: status.to_s}, {id: ids}) #=> error here 
    end 
    end 
end 

class GroupIdsByStatus 
    def self.group(options = {}) 
    result = Hash.new {|h,k| h[k] = []} 
    options.reduce(result) do |buffer, (id, status)| 
     buffer[status.to_sym] << id 
     buffer 
    end 
    result 
    end 
end 

class TasksController < ApplicationController 
    def toggle 
    groups = GroupIdsByStatus.group(params[:tasks]) 
    Task.toggle(current_user, groups) 

    redirect_to tasks_path 
    end 
end 

답변

1

update_all은 유일한 인수로 하나의 해시를받습니다. 당신은 하나의 인자로 업데이트를 모두 넣어해야합니다

user.tasks.update_all(status: status.to_s, id: ids) 

# The above line of code is identical to: 
# user.tasks.update_all({status: status.to_s, id: ids}) # < Notice the curly braces 
이 방법에

더 많은 정보가 나는 것을했던 Rails Relation Docs

+0

에 표시됩니다,하지만 지금은 다른 오류를 보여주는 것 : 액티브 :: StatementInvalid을 TasksController에 #toggle SQLite3 :: MismatchException : 데이터 유형 불일치 : UPDATE "tasks"SET "status"= "완료", "id"= NULL WHERE "tasks". "user_id"=? –

+0

올바른 구문이므로 문제가 해결됩니다. * new * 문제는'ids' 변수가'NULL'으로 인해 발생합니다. 오류를 방지하기 위해'ids'가'NULL'이 아닌지 확인하고 싶을 것입니다. –

+0

감사합니다. Wes Foster! 감사합니다. 고마워. 고마워. –