rails/rails — Issue #4913: scope names starting with 'find_by'
Author: nslocum
Created: 2012-02-06 19:34:45+00:00
State: open

I noticed that if a scope name starts with `find_by`, creating the database and migrating, returns the error:
Mysql2::Error: Table 'cse101_development.users' doesn't exist: SHOW FIELDS FROM \`users\`

To get the error I ran:
rake db:drop
rake db:create
rake db:migrate

The scope is on the User model (user.rb).

scope :find_by_role, lambda { |role|
joins(:roles).where('roles.name = ?', role)
}

If the scope is instead defined as a class method, it works fine

def self.find_by_role role
joins(:roles).where('roles.name = ?', role)
end

The full trace
`$ rake db:migrate --trace`

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
Mysql2::Error: Table 'cse101_development.users' doesn't exist: SHOW FIELDS FROM `users`
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `block in execute'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `execute'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:473:in `columns'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `block (2 levels) in initialize'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:185:in `with_connection'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:92:in `block in initialize'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/base.rb:706:in `yield'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/base.rb:706:in `default'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/base.rb:706:in `columns'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/base.rb:722:in `column_names'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/base.rb:735:in `column_methods_hash'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/base.rb:1101:in `all_attributes_exists?'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/base.rb:886:in `respond_to?'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/named_scope.rb:193:in `valid_scope_name?'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activerecord-3.1.0/lib/active_record/named_scope.rb:175:in `scope'
/code/cse101/app/models/user.rb:41:in `'
/code/cse101/app/models/user.rb:1:in `'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:456:in `load'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:456:in `block in load_file'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:640:in `new_constants_in'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:455:in `load_file'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:342:in `require_or_load'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:489:in `load_missing_constant'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:181:in `block in const_missing'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:179:in `each'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:179:in `const_missing'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/inflector/methods.rb:124:in `block in constantize'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/inflector/methods.rb:123:in `each'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/inflector/methods.rb:123:in `constantize'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:528:in `block in initialize'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:550:in `yield'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:550:in `default'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:550:in `[]'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:595:in `constantize'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/devise-1.5.3/lib/devise.rb:261:in `get'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/devise-1.5.3/lib/devise/mapping.rb:77:in `to'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/devise-1.5.3/lib/devise/mapping.rb:72:in `modules'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/devise-1.5.3/lib/devise/mapping.rb:89:in `routes'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/devise-1.5.3/lib/devise/mapping.rb:156:in `default_used_route'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/devise-1.5.3/lib/devise/mapping.rb:66:in `initialize'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/devise-1.5.3/lib/devise.rb:291:in `new'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/devise-1.5.3/lib/devise.rb:291:in `add_mapping'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/devise-1.5.3/lib/devise/rails/routes.rb:193:in `block in devise_for'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/devise-1.5.3/lib/devise/rails/routes.rb:192:in `each'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/devise-1.5.3/lib/devise/rails/routes.rb:192:in `devise_for'
/code/cse101/config/routes.rb:4:in `block in '
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/actionpack-3.1.0/lib/action_dispatch/routing/route_set.rb:257:in `instance_exec'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/actionpack-3.1.0/lib/action_dispatch/routing/route_set.rb:257:in `eval_block'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/actionpack-3.1.0/lib/action_dispatch/routing/route_set.rb:234:in `draw'
/code/cse101/config/routes.rb:1:in `'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in `load'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in `block in load'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:223:in `block in load_dependency'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:640:in `new_constants_in'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:223:in `load_dependency'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in `load'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/application/routes_reloader.rb:29:in `block in load_paths'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/application/routes_reloader.rb:29:in `each'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/application/routes_reloader.rb:29:in `load_paths'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/application/routes_reloader.rb:13:in `reload!'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/application/routes_reloader.rb:7:in `block in initialize'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/file_update_checker.rb:32:in `call'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/file_update_checker.rb:32:in `execute_if_updated'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/application/finisher.rb:63:in `block (2 levels) in '
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/application/finisher.rb:64:in `call'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/application/finisher.rb:64:in `block in '
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/initializable.rb:25:in `instance_exec'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/initializable.rb:25:in `run'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/initializable.rb:50:in `block in run_initializers'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/initializable.rb:49:in `each'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/initializable.rb:49:in `run_initializers'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/application.rb:92:in `initialize!'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/railtie/configurable.rb:30:in `method_missing'
/code/cse101/config/environment.rb:5:in `'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `require'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `block in require'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:223:in `block in load_dependency'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:640:in `new_constants_in'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:223:in `load_dependency'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `require'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/application.rb:78:in `require_environment!'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/railties-3.1.0/lib/rails/application.rb:189:in `block (2 levels) in initialize_tasks'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/nslocum/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/task.rb:176:in `block in invoke_prerequisites'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `each'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `invoke_prerequisites'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/task.rb:157:in `block in invoke_with_call_chain'
/Users/nslocum/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/gems/rake-0.9.2.2/bin/rake:33:in `'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/bin/rake:19:in `load'
/Users/nslocum/.rvm/gems/ruby-1.9.2-p290@cse101/bin/rake:19:in `

'
Tasks: TOP => db:migrate => environment