My controller seems to have trouble finding the model

in my \app\controllers\business_controller.rb:

class BusinessController < ApplicationController
def search
@businesses = Business.search(params[:businessname], params[:city])
end
end

in my \app\models\business.rb:

class Business < ActiveRecord::Base
def search(businessname, city)
search_condition = “%” + businessname + “%”
find(:all, :conditions => [‘name LIKE ? AND city = ?’,
search_condition, search_condition])
end
end

When I run my app in firefox, I get:

NoMethodError in BusinessController#search

undefined method `search’ for #Class:0x59c69cc

RAILS_ROOT: c:/Ruby/Referme
Application Trace | Framework Trace | Full Trace

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
base.rb:1964:in method_missing' c:/Ruby/Referme/app/controllers/business_controller.rb:3:insearch’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
base.rb:1964:in method_missing' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ base.rb:1322:insend’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:1322:in perform_action_without_filters' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ filters.rb:617:incall_filters’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:610:in perform_action_without_benchmark' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ benchmarking.rb:68:inperform_action_without_rescue’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
core_ext/benchmark.rb:17:in ms' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/ core_ext/benchmark.rb:10:inrealtime’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
core_ext/benchmark.rb:17:in ms' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ benchmarking.rb:68:inperform_action_without_rescue’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
rescue.rb:160:in perform_action_without_flash' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ flash.rb:141:inperform_action’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:523:in send' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ base.rb:523:inprocess_without_filters’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:606:in process' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ base.rb:391:inprocess’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:386:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ routing/route_set.rb:433:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
dispatcher.rb:88:in dispatch' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ dispatcher.rb:111:in_call’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
dispatcher.rb:82:in initialize' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ query_cache.rb:29:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:29:in call' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ connection_adapters/abstract/query_cache.rb:34:incache’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:9:in cache' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ query_cache.rb:28:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
connection_adapters/abstract/connection_pool.rb:361:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ vendor/rack-1.0/rack/head.rb:9:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/methodoverride.rb:24:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ params_parser.rb:15:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
rewindable_input.rb:25:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ session/cookie_store.rb:93:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
reloader.rb:9:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ failsafe.rb:11:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/lock.rb:11:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ vendor/rack-1.0/rack/lock.rb:11:insynchronize’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/lock.rb:11:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ dispatcher.rb:106:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/static.rb:
31:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ vendor/rack-1.0/rack/urlmap.rb:46:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/urlmap.rb:40:in each' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ vendor/rack-1.0/rack/urlmap.rb:40:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/debugger.rb:
17:in call' C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/ log_tailer.rb:17:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/content_length.rb:13:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ vendor/rack-1.0/rack/handler/webrick.rb:46:inservice’
C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in service' C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:173:in start_thread' C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:instart’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in start_thread' C:/Ruby/lib/ruby/1.8/webrick/server.rb:95:instart’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in each' C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:instart’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:23:in start' C:/Ruby/lib/ruby/1.8/webrick/server.rb:82:instart’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/handler/webrick.rb:13:in run' C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:111 C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:ingem_original_require’
C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require’
script/server:3

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
base.rb:1964:in method_missing' c:/Ruby/Referme/app/controllers/business_controller.rb:3:insearch’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:1322:in send' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ base.rb:1322:inperform_action_without_filters’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:617:in call_filters' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ filters.rb:610:inperform_action_without_benchmark’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
benchmarking.rb:68:in perform_action_without_rescue' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/ core_ext/benchmark.rb:17:inms’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
core_ext/benchmark.rb:10:in realtime' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/ core_ext/benchmark.rb:17:inms’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
benchmarking.rb:68:in perform_action_without_rescue' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ rescue.rb:160:inperform_action_without_flash’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
flash.rb:141:in perform_action' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ base.rb:523:insend’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:523:in process_without_filters' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ filters.rb:606:inprocess’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:391:in process' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ base.rb:386:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
routing/route_set.rb:433:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ dispatcher.rb:88:indispatch’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
dispatcher.rb:111:in _call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ dispatcher.rb:82:ininitialize’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:29:in call' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ query_cache.rb:29:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
connection_adapters/abstract/query_cache.rb:34:in cache' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ query_cache.rb:9:incache’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:28:in call' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ connection_adapters/abstract/connection_pool.rb:361:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/head.rb:9:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ vendor/rack-1.0/rack/methodoverride.rb:24:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
params_parser.rb:15:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ rewindable_input.rb:25:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
session/cookie_store.rb:93:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ reloader.rb:9:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
failsafe.rb:11:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ vendor/rack-1.0/rack/lock.rb:11:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/lock.rb:11:in synchronize' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ vendor/rack-1.0/rack/lock.rb:11:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
dispatcher.rb:106:in call' C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/static.rb: 31:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/urlmap.rb:46:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ vendor/rack-1.0/rack/urlmap.rb:40:ineach’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/urlmap.rb:40:in call' C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/debugger.rb: 17:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/
log_tailer.rb:17:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ vendor/rack-1.0/rack/content_length.rb:13:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/handler/webrick.rb:46:in service' C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in run' C:/Ruby/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in start' C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:95:in start' C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:ineach’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in start' C:/Ruby/lib/ruby/1.8/webrick/server.rb:23:instart’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:82:in start' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/ vendor/rack-1.0/rack/handler/webrick.rb:13:inrun’
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:111
C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
gem_original_require' C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:inrequire’
script/server:3

Request

Parameters:

{“businessname”=>“sss”,
“commit”=>“Search”,
“authenticity_token”=>“6otukF4R7SIjMgcXbXyYNFUJZivGGOwSEEsERR/+MHk=”,
“location”=>“ss”}

Show session dump


Response

Headers:

{“Content-Type”=>"",
“Cache-Control”=>“no-cache”}

This looks like the Controller cannot find the Search function in my
model, but apparently the function is defined there. So I am sure what
is going on here.

On Aug 31, 1:09 am, ji [email protected] wrote:

class Business < ActiveRecord::Base
def search(businessname, city)
search_condition = “%” + businessname + “%”
find(:all, :conditions => [‘name LIKE ? AND city = ?’,
search_condition, search_condition])
end
end

This looks like the Controller cannot find the Search function in my
model, but apparently the function is defined there. So I am sure what
is going on here.

You’ve created an instance method, but you’re trying to call it as if
it was a class method (and you probably meant for city to be the
second bind variable in your call to find)

Fred


अभिनव
http://twitter.com/abhinav

Make search a class method, ie, define search like this in your model:

def self.search(business_name, city) # note “self”

do something

end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs