查找东西(但不在生产中)
使用“Advance Rails Recipes”中的教程“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)
任何非常感谢您的帮助。
Using the tutorial "Find Stuff (quick and Dirty)" in "Advance Rails Recipes", I can find stuff in the development environment but not in the production environment.
NoMethodError: undefined method `searchable_by'
I have this code in "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
and this in my 'config/initializers/core_extensions.rb'
ActiveRecord::Base.extend Searchable
I have roamed the internet and found people saying that if I change config.cache_classes to false then it should work: but it doesn't.
I've never tried using extra scripts in my apps, so I'm probably missing out something pretty basic here.
Any help will be appreciated!
UPDATE: Additional Info
I specify this in the model (app/models/candidate.rb)
class Candidate < ActiveRecord::Base
searchable_by :first_name, :surname, :experience, :skills, :looking_for, :hours_required, :location, :more_details
end
And I call it in the controller (app/controllers/candidates_controller.rb)
class CandidatesController < ApplicationController
def index
@candidates = Candidate.visible
end
def search
@candidates = Candidate.visible.search(params[:q])
end
end
(visible is just a named_scope)
Any help would be really appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你在 searchable_by *_column_names 上有一个拼写错误,应该是 *column_names,一旦修复,请务必重新启动你的 Rails 服务器。
you have a typo on searchable_by *_column_names should be *column_names, once that is fixed, be sure to restart your rails server.