我的模型中定义了以下范围:
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范围不能用于数组,或者我做错了什么?
答案 0 :(得分:1)
你必须在required_plugins = %w( vagrant-omnibus vagrant-aws )
required_plugins.each do |plugin|
exec "vagrant plugin install #{plugin};vagrant #{ARGV.join(" ")}" unless Vagrant.has_plugin? plugin || ARGV[0] == 'plugin'
end
方法中加入一个参数。试试这个:
or
UPDATE (ArgumentError:错误的参数数量)
您无法为范围设置默认参数值:
where(user_1: answers).or(where(user_2: answers))
因此,请将其替换为:
scope :answers_in, -> (answers = nil) do
或者如果您希望参数是可选的,请按以下方式定义范围:
scope :answers_in, -> (answers) do
但是你必须将数组元素作为分隔的参数scope :answers_in, -> (*answers) do
而不是AnswerConnection.answers_in(1, 2)
传递。