Home Rails 5: Jquery autocomplete
Reply: 0

Rails 5: Jquery autocomplete

Theopap
1#
Theopap Published in 2017-12-07 13:27:53Z

I have implemented the jquery autocomplete within my rails app.

And this is the setup:

//= require jquery
//= require rails-ujs
//= require jquery-ui
//= require bootstrap
//= require_tree .

items.js

$(document).ready(function() {
    $("#search").autocomplete({
        source: "/search_suggestions",
        autoFocus: true,
        select: function(event, ui) {
            $(event.target).val(ui.item.value);
            $('#search').closest("form").submit();
            return false;
       }
    });
});

this is the search action inside the model items.rb

  def self.search(term)
    return where("0=1") if term !~ /\w{4}/
    where("lower(title) LIKE lower(:term)", term: "%#{term}%")
  end

After that I created a search_suggestions resource:

rails g resource search_suggestion term popularity:integer
rails db:migrate

search_suggestions.rb

class SearchSuggestion < ApplicationRecord

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

  def self.index_items
    Item.find_each do |item|
      index_term(item.title)
      item.title.split.each { |t| index_term(t) }
    end
  end

   def self.index_term(term)
     where(term: term.downcase).first_or_initialize.tap do |suggestion|
     suggestion.increment! :popularity
     suggestion.save!
    end
  end
end

search_suggestions_controller.rb

  def index
    render json: SearchSuggestion.terms_for(params[:term])
  end
end

and I created the following rake task in order to update the search_suggestion index according to the setup I have inside the search_suggestions.rb model:

namespace :search_suggestions do
  desc "Generate search suggestions from items"
  task :index => :environment do
    SearchSuggestion.index_items
  end
end

and if I enter rake search_sugegstions:index the rake finishes without an error but doesn't update the table with the current items. For example there are still items that I have deleted and are still appearing inside the autocomplete.

Any ideas why this is happening? And is there a better way to implement this?

Update1

log/development.log when rake search_suggestions:index

Sorry for the format of this... the log was to big and thus I could not forma it properly.

[1m[36mItem Load (1.1ms)[0m [1m[34mSELECT "items".* FROM "items" ORDER BY "items"."id" ASC LIMIT $1[0m [["LIMIT", 1000]] [1m[36mSearchSuggestion Load (0.5ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (1.2ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.2ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.3ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.5ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.3ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "round"], ["LIMIT", 1]] [1m[35mSQL (0.5ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 19]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.3ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "round"], ["LIMIT", 1]] [1m[35mSQL (0.5ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 19]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.3ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.5ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.3ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.5ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.5ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (6.5ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.3ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.2ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.3ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.7ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.7ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.2ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.3ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.5ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.4ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.8ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.5ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.4ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.8ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.5ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.2ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.5ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m [1m[36mSearchSuggestion Load (0.2ms)[0m [1m[34mSELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT $2[0m [["term", "rolex"], ["LIMIT", 1]] [1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "search_suggestions" SET "popularity" = COALESCE("popularity", 0) + 1 WHERE "search_suggestions"."id" = $1[0m [["id", 20]] [1m[35m (0.1ms)[0m [1m[35mBEGIN[0m [1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299589 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO