Rails 3 Concern Modules for Models

Viewed 9360

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
3 Answers
Related