0
我在我的模型中定義的範圍如下:範圍不接受數組作爲參數
scope :answers_in, -> (answers = nil) do
return unless answers.present?
where(answer_1: answers).or(where(answer_2: answers))
end
的answers
應該由數組。然而,當我撥打電話傳遞一個陣列,接收參數錯誤:
AnswerConnection.answers_in([1, 2])
ArgumentError: wrong number of arguments (given 2, expected 0..1)
from /vagrant/farma_alg_reborn/app/models/answer_connection.rb:7:in `block in <class:AnswerConnection>'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activerecord-5.0.1/lib/active_record/scoping/named.rb:159:in `instance_exec'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activerecord-5.0.1/lib/active_record/scoping/named.rb:159:in `block (2 levels) in scope'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activerecord-5.0.1/lib/active_record/relation.rb:351:in `scoping'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activerecord-5.0.1/lib/active_record/scoping/named.rb:159:in `block in scope'
from (irb):2
from /usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.0.1/lib/rails/commands/console.rb:65:in `start'
from /usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from /usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `block in require'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /vagrant/farma_alg_reborn/bin/rails:9:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `block in load'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
from /usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.0/lib/spring/commands/rails.rb:6:in `call'
from /usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.0/lib/spring/command_wrapper.rb:38:in `call'
from /usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.0/lib/spring/application.rb:191:in `block in serve'
from /usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.0/lib/spring/application.rb:161:in `fork'
from /usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.0/lib/spring/application.rb:161:in `serve'
from /usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.0/lib/spring/application.rb:131:in `block in run'
from /usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.0/lib/spring/application.rb:125:in `loop'
from /usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.0/lib/spring/application.rb:125:in `run'
from /usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.0/lib/spring/application/boot.rb:19:in `<top (required)>'
from /usr/local/rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
這是一個錯誤,Rails的範圍不使用數組或我做錯了什麼?
我更新我的答案(並將'user_x'更改爲'answer_x',我偶然切換)。這又給我一個參數錯誤。 – rwehresmann
哪一個是你的AnswerConnection模型中的第七行? (在'中的block中回答'from /vagrant/farma_alg_reborn/app/models/answer_connection.rb:7') –
這是範圍聲明:'scope:answers_in, - >(answers = nil) ' – rwehresmann