English 中文(简体)
在Rubya的铁路搜查—— 我如何寻找每一字,而不是确切的措辞?
原标题:Searching in Ruby on Rails - How do I search on each word entered and not the exact string?

我在铁路上安装了博客应用程序,我正试图实施搜索功能。 博客申请允许用户担任对口员额。 标签在自己的桌子和belong_to :post上设立。 标的创建时,在标的表中,标的名号为正名,与职位挂钩。 塔格斯正在铺设。

我试图允许用户按任何顺序搜索任何字标。 这就是我所说的话。 让我说,某个职位有一个标签,即瓦解代码控制器。 在我目前的搜索特征中,如果用户搜索废墟、废墟代码或废气密码控制器,就会发现标签。 如果垃圾控制器中的用户类型,则不会发现。

基本上,我说的是,我喜欢在搜索过程中的每一字,而不一定是进入搜索过程中的扼杀。

我一直在尝试提供多个文本领域,使用户能够用多种语言进行分类,而且一直在使用以下代码,但似乎无法达到上述目的。 如果这是一个明显的问题,在安装灰色或皮面粉之前,我想我会检查一下是否有一个简单的固定点,那么,我会看到这种 rub。 我的守则是:

观点:/views/tags/index.html.erb

<% form_tag tags_path, :method =>  get  do %>
    <p>
      <%= text_field_tag :search, params[:search], :class => "textfield-search" %>
      <%= submit_tag "Search", :name => nil, :class => "search-button" %>
    </p>
  <% end %>

主计长

 def index
    @tags = Tag.search(params[:search]).paginate :page => params[:page], :per_page => 5
    @tagsearch = Tag.search(params[:search])
    @tag_counts = Tag.count(:group => :tag_name, 
       :order =>  count_all DESC , :limit => 100)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @tags }
    end
  end

租户模式

class Tag < ActiveRecord::Base
  belongs_to :post
  validates_length_of :tag_name, :maximum=>42
  validates_presence_of :tag_name

  def self.search(search)
    if search
      find(:all, :order => "created_at DESC", :conditions => [ tag_name LIKE ? , "%#{search}%"])
    else
      find(:all, :order => "created_at DESC")
    end
  end

end
最佳回答

如果我正确理解你的问题,如果浏览标语与在座标语中所说的话之一吻合,你就希望返回一行。

页: 1 方法如下:

def self.search(search)
  all :conditions =>  (search ? { :tag_name => search.split} : [])
end

如果需要部分配对,则采取下列行动:

def self.search(str)
  return [] if str.blank?
  cond_text   = str.split.map{|w| "tag_name LIKE ? "}.join(" OR ")
  cond_values = str.split.map{|w| "%#{w}%"}
  all(:conditions =>  (str ? [cond_text, *cond_values] : []))
end

Edit 1 If you want pass multiple search strings then:

def self.search(*args)
  return [] if args.blank?
  cond_text, cond_values = [], []
  args.each do |str|
    next if str.blank?  
    cond_text << "( %s )" % str.split.map{|w| "tag_name LIKE ? "}.join(" OR ")
    cond_values.concat(str.split.map{|w| "%#{w}%"})
  end
  all :conditions =>  [cond_text.join(" AND "), *cond_values]
end

现在你们可以发出这样的呼吁:

Tag.search("Ruby On Rails")

Tag.search("Ruby On Rails", "Houston")

Tag.search("Ruby On Rails", "Houston", "TX")

Tag.search("Ruby On Rails", "Houston", "TX", "Blah")

Tag.search("Ruby On Rails", "Houston", "TX", "Blah", ....) # n parameters

<><>Caveat:

野心卡LIKE查询效率不高(因为它们没有使用索引)。 如果你掌握大量数据,你应考虑使用Sphinx(通过思考Sphinx)OR Solr(通过SunSpot)。

问题回答

页: 1 您可以在你控制职位上做一些编码:。

<pre>
def show
    @post = Post.find(params[:id])
    @tag_counts = Tag.count(:group => :name, :order =>  updated_at DESC , :limit => 10)
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end
</pre>

2.Now对你的观察档案做了一些改动:

  <pre>
  <b>Tags:</b>
  <%= join_tags(@post) %>
  <%unless @tag_counts.nil?%>
  <% @tag_counts.each do |tag_name, tag_count| %> 
  <tr><td><%= link_to(tag_name, posts_path(:name => tag_name)) %></td>
  <td>(<%=tag_count%>)</td>
  </tr><% end %>
  <%end%>
</pre> 

3。 一个重要事项是,在“





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...