Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 764251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:41:41+00:00 2026-05-14T16:41:41+00:00

I have built a blog application w/ ruby on rails and I am trying

  • 0

I have built a blog application w/ ruby on rails and I am trying to implement a search feature. The blog application allows for users to tag posts. The tags are created in their own table and belong_to :post. When a tag is created, so is a record in the tag table where the name of the tag is tag_name and associated by post_id. Tags are strings.

I am trying to allow a user to search for any word tag_name in any order. Here is what I mean. Lets say a particular post has a tag that is ‘ruby code controller’. In my current search feature, that tag will be found if the user searches for ‘ruby’, ‘ruby code’, or ‘ruby code controller’. It will not be found if the user types in ‘ruby controller’.

Essentially what I am saying is that I would like each word entered in the search to be searched for, not necessarily the ‘string’ that is entered into the search.

I have been experimenting with providing multiple textfields to allow the user to type in multiple words, and also have been playing around with the code below, but can’t seem to accomplish the above. I am new to ruby and rails so sorry if this is an obvious question and prior to installing a gem or plugin I thought I would check to see if there was a simple fix. Here is my code:

View: /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 %>

TagsController

 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

Tag Model

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T16:41:42+00:00Added an answer on May 14, 2026 at 4:41 pm

    If I read your problem correctly, you want to return a row if the tag names for the row matches one of the words passed in the query string.

    You can rewrite your search method as follows:

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

    If you need partial matching then do the following:

    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
    

    Now you can make calls such as:

    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:

    The wild card LIKE searches are not very efficient(as they don’t use the index). You should consider using Sphinx (via ThinkingSphinx) OR Solr(via SunSpot) if you have lot of data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.