First off, I’m following the practice found here for Rails concerns (great idea!): https://gist.github.com/1014971
I’m getting an error though:
undefined method `search' for #<Class:0x5c25ea0>
app/controllers/accessories_controller.rb:6:in `index'
I DO have my /app/models/concerns/ directory loaded in /config/application.rb. So the “concern” modules ARE being loaded. Just wanted to point that out.
Here is my code:
/app/models/concerns/searchable.rb
module Searchable
extend ActiveSupport::Concern
# Add a "search" scope to the models
def self.search (search)
if search
where('name LIKE ?', "%#{search}%")
else
scoped
end
end
end
/app/models/accessory.rb
class Accessory < ActiveRecord::Base
include Searchable
...
end
/app/controllers/accessories_controller.rb
class AccessoriesController < ApplicationController
def index
@accessories = Accessory.search(params[:search])
...
end
end
Well, with a little more toying around I figured out what is wrong!
When you’re wanting to directly modify the Model from within the Module (concern), you need to wrap the functionality inside an included block.
I’ve changed my concern module to the following:
That is it! Hopefully this will help someone else with the same quesion!