"고급 레일 요리법"의 "Find Stuff (Quick and Dirty)"자습서를 사용하면 개발 환경에서 물건을 찾을 수 있지만 프로덕션 환경에서는 찾을 수 없습니다.자료 찾기 (생산 중이 아님)
NoMethodError: undefined method `searchable_by'
내 '설정/초기화/core_extensions.rb'
ActiveRecord::Base.extend Searchable
이 "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.cache_classes를 false로 변경하면 작동해야한다고 말하는 사람들이 있습니다. 그러나 그렇지 않습니다.
나는 애플 리케이션에서 여분의 스크립트를 사용 해본 적이 없기 때문에 여기서는 아주 기본적인 것을 놓치고있을 것이다.
도움이 될 것입니다!
UPDATE :
정보 추가 나는 (응용 프로그램/모델/candidate.rb)
class Candidate < ActiveRecord::Base
searchable_by :first_name, :surname, :experience, :skills, :looking_for, :hours_required, :location, :more_details
end
모델이를 지정 그리고 컨트롤러 (응용 프로그램/컨트롤러/candidates_controller에 문의하십시오. RB)
class CandidatesController < ApplicationController
def index
@candidates = Candidate.visible
end
def search
@candidates = Candidate.visible.search(params[:q])
end
end
(표시 단지 named_scope이다)
도움이 될 것입니다.
searchable_by를 어떻게 부르십니까? – shingara
@candidates = Candidate.search (params [: q]) –