Scope doesn't receive array as argument - ruby-on-rails

I have the following scope defined in my model:
scope :answers_in, -> (answers = nil) do
return unless answers.present?
where(answer_1: answers).or(where(answer_2: answers))
end
The answers should by an array. However, when I make a call passing an array, receive an argument error:
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>'
Is this a bug, Rails scopes doesn't work with arrays or I'm doing something wrong?

You have to put an argument into the or method. Try this:
where(user_1: answers).or(where(user_2: answers))
UPDATE (ArgumentError: wrong number of arguments)
You can't set a default parameter value for scopes:
scope :answers_in, -> (answers = nil) do
So, replace it with:
scope :answers_in, -> (answers) do
Or if you want the argument to be optional, define the scope like this:
scope :answers_in, -> (*answers) do
But then you have to pass array elements as separated arguments AnswerConnection.answers_in(1, 2) instead of AnswerConnection.answers_in([1, 2]).

Related

finding model in rails gives "ArgumentError: no receiver given"

I just change the name of one of my models, it used to be "Course", and now it's "Booking", also I have created two brand new classes called "Course" and "Lesson" that both inherit from "Booking"
I have updated all the has_many in my models from :courses to :bookings, I made sure to rename the file to booking.rb, and renamed the table in the database to bookings.
When I run this in the rails console
Booking.find 1
I get this error
ArgumentError: no receiver given
The 2nd line below is the one causing the error
if filter.arity <= 0
lambda { |target, _| target.instance_exec(&filter) }
else
lambda { |target, _| target.instance_exec(target, &filter) }
end
Here is the stack trace
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/callbacks.rb:396:in `instance_exec'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/callbacks.rb:396:in `block in make_lambda'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/callbacks.rb:207:in `block in halting_and_conditional'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/callbacks.rb:456:in `block in call'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/callbacks.rb:456:in `each'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/callbacks.rb:456:in `call'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/callbacks.rb:101:in `__run_callbacks__'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/callbacks.rb:750:in `_run_initialize_callbacks'
from ~/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0.1/lib/active_record/core.rb:350:in `init_with'
from ~/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0.1/lib/active_record/persistence.rb:69:in `instantiate'
from ~/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0.1/lib/active_record/querying.rb:50:in `block (2 levels) in find_by_sql'
from ~/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0.1/lib/active_record/result.rb:52:in `block in each'
from ~/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0.1/lib/active_record/result.rb:52:in `each'
from ~/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0.1/lib/active_record/result.rb:52:in `each'
from ~/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0.1/lib/active_record/querying.rb:50:in `map'
from ~/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0.1/lib/active_record/querying.rb:50:in `block in find_by_sql'
... 14 levels...
from ~/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from ~/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from ~/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from ~/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
from ~/fidka_app/bin/rails:38:in `<top (required)>'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `block in load'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from ~/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
from ~/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from ~/.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>'

Is there something wrong with this Rails Console error message?

Very new to Rails, so bear with me -
Currently paranoid that my ruby and gem versions aren't up to date because I'd occasionally get error messages when running rails test. Previously had rvm and rbenv both installed, but wow I've removed both and reinstalled rvm only.
When I go into a irb and call an action that doesn't work, such as: "foo".select, I get a simple error message:
NoMethodError: private method 'select' called for "foo":String
from (irb):4
from /Users/Joseph/.rvm/rubies/ruby-2.3.1/bin/irb:11:in '<main>'
However, when I type the same command "foo".select in rails console instead, I get:
NoMethodError: private method `select' called for "foo":String
from (irb):1
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
from /Users/Joseph/workspace/sample_app/bin/rails:9:in `<top (required)>'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `block in load'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in `call'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in `call'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application.rb:191:in `block in serve'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application.rb:161:in `fork'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application.rb:161:in `serve'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'
from /Users/Joseph/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/Joseph/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
Is this normal? Or is there something wrong with my gems here?
There's nothing wrong with your gems. The thing is that when you run irb, it's just that. You only run the ruby interactive.
When you run rails console in order to show you the console it needs to go and set it for you with ActiveRecord, rails core, etc. So this (below) is all it does before it's set.
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
from /Users/Joseph/workspace/sample_app/bin/rails:9:in `<top (required)>'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `block in load'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in `call'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in `call'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application.rb:191:in `block in serve'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application.rb:161:in `fork'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application.rb:161:in `serve'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
from /Users/Joseph/.rvm/gems/ruby-2.3.1#sample_app/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'
from /Users/Joseph/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/Joseph/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
Notice that irb is executed really fast, while rails console takes its time, depending on your initializers, configurations, etc.

Unwanted lines in rails console error reporting

?> error
NameError: undefined local variable or method `error' for main:Object
from (irb):7
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/railties-4.2.0.beta4/lib/rails/commands/console.rb:110:in `start'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/railties-4.2.0.beta4/lib/rails/commands/console.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/railties-4.2.0.beta4/lib/rails/commands/commands_tasks.rb:68:in `console'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/railties-4.2.0.beta4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/railties-4.2.0.beta4/lib/rails/commands.rb:17:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:252:in `require'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:252:in `block in require'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:237:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:252:in `require'
from /home/ubuntu/workspace/sample_app/bin/rails:8:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:246:in `load'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:246:in `block in load'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:237:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:246:in `load'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/spring-1.1.3/lib/spring/commands/rails.rb:6:in `call'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/spring-1.1.3/lib/spring/command_wrapper.rb:38:in `call'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/spring-1.1.3/lib/spring/application.rb:180:in `block in serve'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/spring-1.1.3/lib/spring/application.rb:153:in `fork'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/spring-1.1.3/lib/spring/application.rb:153:in `serve'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /usr/local/rvm/gems/ruby-2.1.4#rails4/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
In rails console when I commit an error, the console displays tons of lines which I don't want to see. Can this be silenced?
I've found that you can add this line:
Rails.backtrace_cleaner.add_silencer { |line| line =~ /rvm/ }
in config/initializers/backtrace_silencers.rb but this doesn't resolve the problem.

Not a single command is working with rails app

Whatever command like rake,rails etc I issue,I get the same error in my rails app.
Block not supplied (ArgumentError)
C:\Sites\merevik\merevik>rails console
C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/ra
iltie/configurable.rb:24:in `class_eval': block not supplied (ArgumentError)
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/railtie/configurable.rb:24:in `configure'
from C:/Sites/merevik/merevik/config/initializers/setup_mail.rb:2:in `bl
ock in <top (required)>'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/railtie/configurable.rb:24:in `class_eval'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/railtie/configurable.rb:24:in `configure'
from C:/Sites/merevik/merevik/config/initializers/setup_mail.rb:1:in `<t
op (required)>'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.1/lib/active_support/dependencies.rb:245:in `load'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.1/lib/active_support/dependencies.rb:245:in `block in load'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.1/lib/active_support/dependencies.rb:236:in `load_dependency'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.1/lib/active_support/dependencies.rb:245:in `load'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/engine.rb:588:in `block (2 levels) in <class:Engine>'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/engine.rb:587:in `each'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/engine.rb:587:in `block in <class:Engine>'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/initializable.rb:30:in `instance_exec'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/initializable.rb:30:in `run'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/initializable.rb:55:in `block in run_initializers'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/initializable.rb:54:in `each'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/initializable.rb:54:in `run_initializers'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/application.rb:136:in `initialize!'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/railtie/configurable.rb:30:in `method_missing'
from C:/Sites/merevik/merevik/config/environment.rb:5:in `<top (required
)>'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.1/lib/active_support/dependencies.rb:251:in `require'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.1/lib/active_support/dependencies.rb:251:in `block in require'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.1/lib/active_support/dependencies.rb:236:in `load_dependency'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.1/lib/active_support/dependencies.rb:251:in `require'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/application.rb:103:in `require_environment!'
from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/railties-3.2.1
/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
It appears that you are calling configure from line 2 of setup_mail.rb without passing a block. On line 1, you are also calling configure, and passing the block which contains the faulty call on line 2. I am guessing you are doing something like:
configure.some_option = 'value'
where you really meant:
config.some_option = 'value'
This is the source for configure:
def configure(&block)
class_eval(&block)
end
Calling class_eval without a block will always throw an ArgumentError.

Define meta class methods using modules

I want to be able to create meta class method. For exemple, I have an autocomplete mechanisme on my website working with select_tags.
Here is how I would like to build my select options:
f.select(:subjects, options_from_collection_for_select(Subject.autocomplete_for_subject_title, :id, :title))
So my autocomplete_for_subject_title class method could return for now Subject.all
I was thinking about a module than defines a class method for a given model, something like this :
Subject.rb
class Subject
include SimpleAutocomplete
field :title, :type => String
autocomplete_for :subject, :title
end
lib/simple_autocomplete.rb
module SimpleAutocomplete
def self.included(base)
base.class_eval do |klass|
extend ClassMethods
end
end
module ClassMethods
def autocomplete_for(object, method, options = {}, &block)
(class << object; object; end).instance_eval { define_method "autocomplete_for_#{object}_#{method}" do
puts "It works!"
end
}
end
end
end
But when I launch rails server, here is what I've got :
simple_autocomplete.rb:13:in `autocomplete_for': can't define singleton (TypeError)
from /Users/pierrelouisgottfrois/Work/teamento_beta_git/app/models/subject.rb:20:in `<class:Subject>'
from /Users/pierrelouisgottfrois/Work/teamento_beta_git/app/models/subject.rb:1:in `<top (required)>'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:454:in `load'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:454:in `block in load_file'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:596:in `new_constants_in'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:453:in `load_file'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:340:in `require_or_load'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:300:in `depend_on'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:216:in `require_dependency'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/rails/mongoid.rb:55:in `load_model'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/rails/mongoid.rb:18:in `block (2 levels) in load_models'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/rails/mongoid.rb:17:in `each'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/rails/mongoid.rb:17:in `block in load_models'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/paths.rb:102:in `block in each'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/paths.rb:102:in `each'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/paths.rb:102:in `each'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/rails/mongoid.rb:16:in `load_models'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/railtie.rb:86:in `block (2 levels) in <class:Railtie>'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/callbacks.rb:419:in `_run_prepare_callbacks'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/callbacks.rb:40:in `initialize'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/stack.rb:33:in `new'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/stack.rb:33:in `build'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/stack.rb:79:in `block in build'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/stack.rb:79:in `each'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/stack.rb:79:in `inject'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.4/lib/action_dispatch/middleware/stack.rb:79:in `build'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/application.rb:162:in `app'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/application/finisher.rb:35:in `block in <module:Finisher>'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/initializable.rb:25:in `instance_exec'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/initializable.rb:25:in `run'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/initializable.rb:50:in `block in run_initializers'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/initializable.rb:49:in `each'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/initializable.rb:49:in `run_initializers'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/application.rb:134:in `initialize!'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/application.rb:77:in `method_missing'
from /Users/pierrelouisgottfrois/Work/teamento_beta_git/config/environment.rb:10:in `<top (required)>'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:239:in `require'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:239:in `block in require'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:225:in `block in load_dependency'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:596:in `new_constants_in'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:225:in `load_dependency'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:239:in `require'
from /Users/pierrelouisgottfrois/Work/teamento_beta_git/config.ru:3:in `block in <main>'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
from /Users/pierrelouisgottfrois/Work/teamento_beta_git/config.ru:1:in `new'
from /Users/pierrelouisgottfrois/Work/teamento_beta_git/config.ru:1:in `<main>'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/builder.rb:35:in `eval'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/builder.rb:35:in `parse_file'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/server.rb:162:in `app'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/server.rb:248:in `wrapped_app'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/server.rb:213:in `start'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands/server.rb:65:in `start'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands.rb:30:in `block in <top (required)>'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands.rb:27:in `tap'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands.rb:27:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I've read this blog post to start: http://blog.jayfields.com/2007/10/ruby-defining-class-methods.html
Do you have any idea ?
You're trying to invoke the singleton class of object, via class << object - but object is the symbol :subject, which I don't think is what you want.
I'm not sure what you do want, mind you, but that might help you figure it out.

Resources