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 8020615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:44:16+00:00 2026-06-04T21:44:16+00:00

I’ve implemented a simple search form (according to the simple form screencast) that searches

  • 0

I’ve implemented a simple search form (according to the “simple form” screencast) that searches the “illnesses” table in my DB.
Now I want the same search box to search both the “illnesses” table and the “symptoms” table.

My code currently looks like this:

main_page\index.html.erb:

<b>Illnesses</b>
    <%= form_tag illnesses_path, :method => 'get' do %>
      <p>
        <%= text_field_tag :search, params[:search] %><br/>
        <%= submit_tag "Illnesses", :name => nil %><br/>
      </p>

illnesses_controller.rb:

class IllnessesController < ApplicationController
    def index
    @illnesses = Illness.search(params[:search])

    respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @illnesses }
    end
    ...
end

illness.rb:

class Illness < ActiveRecord::Base
    ...

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

Could you please guide me how to implement this extension?
I’m a beginner (obviously) and I don’t really know what should be the “form_tag” action, where should I implement it and which class should implement the extended search…

Thanks,
Li

  • 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-06-04T21:44:17+00:00Added an answer on June 4, 2026 at 9:44 pm

    Hmm, to just easily set off presuming you have a Symptom class similar to the Illness class(btw it would be most clean if you refactored the search functionality into a module and then include this module in both classes) then you can do:

    class IllnessesController < ApplicationController
      def index
        @results = Illness.search(params[:search]) + Symptom.search(params[:search])
        ...
      end
    end
    

    But maybe you would like to refactor the name of the controller, because now it is not anymore Illness specific. Also notice that we are here using two search queries instead of one so it is not optimal, but saves you the pain of sending a pure SQL query for two types of models at the same time.

    Okay for the module. If you are not familiar with modules they might seem a little weird but they are little more than a piece of code that can be shared across classes to keep things DRY which is our case too. You can imagine including the module as taking the code from the module and evaluating it (courtesy of interpreted languages) in the context of the class which has the same result as if the module code was hard coded into the class itself. So the module looks like:

    module Search
      def self.search(token)
        find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
      end
    end
    

    And now any class, if it implements the find method(ActiveRecord API) can happily include this module and enjoy the search functionality like this:

    require 'path/to/search'
    
    class Foo < ActiveRecord::Base
      include Search
    end
    

    That’s it. Now if you need to tweak your search code, you change it in one place and it propagates to all the includers. You can also create modules inside modules which is sometimes used like this:

    require 'active_support/concern'
    
    module Search
      extend ActiveSupport::Concern
    
      included do
        #Stuff that gets done when the module is included (new validations, callbacks etc.)
      end
    
      module ClassMethods
        #Here you define stuff without the self. prefix
      end
    
      module InstanceMethods
        #Instance methods here
      end
    end
    

    But some of it is convention that is defined in ActiveSupport::Concern so not everything will probably work in pure ruby. I encourage you to experiment with these things, they make ruby real fun. Oh, extend is very much like include, only, as I understand it, it kind of evaluates on the class level. So all instance methods of the included module would become class methods of the includer module if you follow me. Have fun!

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

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.