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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:35:15+00:00 2026-06-18T15:35:15+00:00

I am following Railscasts #399 to implement autocompletion in my search box using autocomplete

  • 0

I am following Railscasts #399 to implement autocompletion in my search box using autocomplete from jquery-ui. However, I would also like to be able include which category each search suggestion belongs to and group the suggestions, similar to how Pandora can match on Song/Artist/Album.

My model looks like this:

class SearchSuggestion < ActiveRecord::Base
  attr_accessible :popularity, :term, :type

  def self.terms_for(prefix)
     suggestions = where("term like ?", "#{prefix}_%")
     suggestions.order("popularity desc").limit(10).pluck(:term)
  end
end

and my controller like this:

class SearchSuggestionsController < ApplicationController
  def index
    render json: SearchSuggestion.terms_for(params[:term])
  end
end

How can I modify terms_for so that I can include :type in my json in a format that autocomplete can handle, and how would I go about rendering this?

  • 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-18T15:35:16+00:00Added an answer on June 18, 2026 at 3:35 pm

    I found a way to do what I wanted, so I figured I would answer my own question. I discovered the handy collect method, and wrote my function like this:

    def self.terms_for(prefix)
       suggestions = where("term like ?", "#{prefix}_%").order("popularity desc").limit(10)
       suggestions.collect {|suggestion| {"value" => suggestion["term"], "type" => suggestion["suggestion_type"] }}
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im following the railscasts using jquery tokeninput http://railscasts.com/episodes/258-token-fields-revised in creating autocomplete tag tokens and
I am following this video http://railscasts.com/episodes/258-token-fields-revised and i implement this also sucessfully. But now
I'm using the code from Railscasts 189 to implement roles with Devise so I
I'm using a nested form following rbates approach: http://railscasts.com/episodes/196-nested-model-form-revised I'm also using Twitter Bootstrap.
I'm following the Railscasts tutorial on using OpenID with AuthLogic . This command: $
I've seen plenty of other questions about jQuery UI Autocomplete but none exactly like
I have been following Railscasts guide for converting my db from sqlite to postgreSQl.
I'm following Ryan Bates railscasts on password reseting. I decided to implement his code
I need help with my jcrop preview. I am following this tutorial from railscasts
I'm following http://railscasts.com/episodes/250-authentication-from-scratch for simple authentication. It works as expected. I have a model

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.