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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:07:17+00:00 2026-06-03T14:07:17+00:00

I have a form with an auto-complete field that uses jQuery Tokeninput . The

  • 0

I have a form with an “auto-complete” field that uses jQuery Tokeninput. The basic query, which just looks for Users whose name or username matches the typed text, is:

#users_controller.rb
def index
  @users = User.where("LOWER(name) like ? OR LOWER(username) like ?", "%#{params[:q].downcase}%", "%#{params[:q].downcase}%").order('name ASC').limit(10) if params[:q]

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @users, :only => [:id, :name, :username] }

end

This works well.

My app also has a “follower model”, and I want to limit which users show up in the autocomplete field so that only “reciprocal followers” are returned. A “reciprocal follower” is someone I follow who follows me back [(A follows B) AND (B follows A)].

I have a method in the User model that returns reciprocal followers for a user:

#user.rb
# Users relation to Relationships
has_many :relationships, :foreign_key => "follower_id", :dependent => :destroy
has_many :followed_users, :through => :relationships, :source => :followed
has_many :reverse_relationships, :foreign_key => "followed_id",
                                 :class_name  => "Relationship",
                                 :dependent => :destroy
has_many :followers, :through => :reverse_relationships, :source => :follower

def reciprocal_followers
  self.followers & self.followed_users
end

I can get reciprocal followers for the person typing the text:

@reciprocal_followers = current_user.reciprocal_followers

But how do I pare that list down according to what text has been typed? How do I integrate the existing autocomplete (@users = User.where…) query with @reciprocal_followers?

In English: “I want autocomplete to show a list of up to 10 reciprocal users whose name or username is like whatever the user has been typed in the autocomplete field”.

How do I modify the query defining “@users” to limit according to “@reciprocal followers”?

  • 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-03T14:07:19+00:00Added an answer on June 3, 2026 at 2:07 pm

    Here’s what I ended up working out:

    @users = current_user.reciprocal_followers.select {|u| u.name.downcase =~ /#{params[:q].downcase}/ || u.username.downcase =~ /#{params[:q].downcase}/}.sort_by { |u| [u.name, u.username] }.first(10) if params[:q]
    

    A friend also recommended I move this out of users_controller.rb and into relationships_controller.rb where I have “follow” and “unfollow” actions since I’m really looking for a list of relations that happen to be users (and not just looking to return users). If I leave it in users_controller.rb, I should probably move it out of the “index” action.

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

Sidebar

Related Questions

I have a form field which uses Jorn Zaefferer's autocomplete jQuery plugin. If i
I have a field accountname that uses Jquery autosuggest in a textbox. Here as
I have a form with a Jquery plugin requirement that creates a form input
I have the following jquery Autocomplete form that pulls data in from a local
I have a simple login form that captures username/password with some simple Javascript (JQuery)
I have been trying to update a text field in a form that will
I have a simple jquery auto complete search in my Rails 3 app based
I have this form. Basically what I want is to send a auto-response with
I have a simple form on a page and I want to auto submit
I have a Rails form for email, and I would like to have auto-completion

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.