ruby-on-railsrubyfull-text-searchsunspot-railscomfortable-mexican-sofa

extending model in comfortable_mexican_matress


I'm trying to implement full-text search in my rails application. The application is based on comfortable_mexican_sofa. For the full-text I am using the sunspot gem. I need to add add the searchable attribute to the Pages class. I created a new file under models with the same class but it does not seem to be working. How can I extend this class? I am getting the error

`undefined method `search' for #<Class:0x007fbe90f6faa8>`

app/models/Page.rb

class Comfy::Cms::Page < ActiveRecord::Base
  searchable do
    text :label, :content_cache
    boolean :featured
  end
end

articles_controller.rb

def index
    since = params[:sinceDate]
    query = params[:searchQuery]
    @articles = nil
    if query
      @articles = Comfy::Cms::Page.search{ fulltext query }.published.all
    else
      @articles = Comfy::Cms::Page.published.all
    end
    if since
      @articles = @articles.reject{ |a| a[:created_at] < Date.parse(since) }
    end
    # @articles
    # if query
    #   @articles = @articles.select{ |a| a[:label].match(/#{query}/i) }
    # end
  end

Solution

  • You can trying doing this:

    Make a file and put it into your initializers folder:

    Comfy::Cms::Page.class_eval do
      searchable do
        text :label, :content_cache
        boolean :featured
      end
    end