【问题标题】:Find Stuff (but not in production)查找资料(但不在生产中)
【发布时间】:2010-08-16 09:48:32
【问题描述】:

使用“Advance Rails 食谱”中的教程“Find Stuff (quick and Dirty)”,我可以在开发环境中找到东西,但在生产环境中找不到。

NoMethodError: undefined method `searchable_by'

我在“lib/searchable.rb”中有这段代码

module Searchable

  def searchable_by(*_column_names)
    @search_columns = []
    [column_names].flatten.each do |name|
      @search_columns << name
    end
  end

  def search(query, fields=nil, options={})
    with_scope :find => {
      :conditions => search_conditions(query, fields) } do
      find :all, options
      end
  end

  def search_conditions(query, fields=nil)
    return nil if query.blank?
    fields ||= @search_columns

    words = query.split(",").map(&:split).flatten

    binds = {}
    or_frags = []
    count = 1

    words.each do |word|
      like_frags = [fields].flatten.map { |f| "LOWER(#{f}) LIKE :word#{count}" }
      or_frags << "(#{like_frags.join(" OR ")})"
      binds["word#{count}".to_sym] = "%#{word.to_s.downcase}%"
      count += 1
    end

    [or_frags.join(" AND "), binds]
  end
end

这在我的 'config/initializers/core_extensions.rb' 中

ActiveRecord::Base.extend Searchable

我在互联网上漫游,发现有人说如果我将 config.cache_classes 更改为 false 那么它应该可以工作:但它没有。

我从未尝试在我的应用程序中使用额外的脚本,所以我可能在这里遗漏了一些非常基本的东西。

任何帮助将不胜感激!

更新:附加信息

我在模型中指定了这个(app/models/candidate.rb)

class Candidate < ActiveRecord::Base
  searchable_by :first_name, :surname, :experience, :skills, :looking_for, :hours_required, :location, :more_details
end

我在控制器中调用它 (app/controllers/candidates_controller.rb)

class CandidatesController < ApplicationController
  def index
    @candidates = Candidate.visible
  end

  def search
    @candidates = Candidate.visible.search(params[:q])
  end

end

(可见只是一个named_scope)

任何帮助将不胜感激。

【问题讨论】:

  • @candidates = Candidate.search(params[:q])

标签: ruby-on-rails ruby search activerecord production-environment


【解决方案1】:

您在 searchable_by *_column_names 上的拼写错误应该是 *column_names,一旦修正,请务必重新启动您的 Rails 服务器。

【讨论】:

    猜你喜欢
    • 2017-05-25
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2014-03-25
    • 2015-02-06
    • 1970-01-01
    • 2017-11-08
    相关资源
    最近更新 更多