Created
April 18, 2016 10:51
-
-
Save andreas-it-dev/b76aa31bde58909fe024210da9439c84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):021:0> SearchSuggestion.index_charts | |
/home/andreas/railsdev/chartbook/app/models/search_suggestion.rb:10: warning: toplevel constant Chart referenced by ChartsController::Chart | |
Chart Load (0.8ms) SELECT "charts".* FROM "charts" ORDER BY "charts"."id" ASC LIMIT 1000 | |
SearchSuggestion Load (0.5ms) SELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT 1 [["term", "test"]] | |
SearchSuggestion Load (0.3ms) SELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT 1 [["term", "test"]] | |
SearchSuggestion Load (0.3ms) SELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT 1 [["term", "qeq"]] | |
(0.2ms) BEGIN | |
SQL (0.5ms) INSERT INTO "search_suggestions" ("popularity", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["popularity", 1], ["created_at", "2016-04-18 10:48:58.380611"], ["updated_at", "2016-04-18 10:48:58.380611"]] | |
(4.3ms) COMMIT | |
SearchSuggestion Load (0.4ms) SELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = $1 ORDER BY "search_suggestions"."id" ASC LIMIT 1 [["term", "qeq"]] | |
(0.1ms) BEGIN | |
SQL (0.3ms) INSERT INTO "search_suggestions" ("popularity", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["popularity", 1], ["created_at", "2016-04-18 10:48:58.389364"], ["updated_at", "2016-04-18 10:48:58.389364"]] | |
(2.7ms) COMMIT | |
=> nil | |
irb(main):022:0> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1,Test,Body | |
2,qeq,qweew |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1,NULL,1,"2016-04-18 09:15:10.331628","2016-04-18 09:15:10.331628" | |
2,NULL,1,"2016-04-18 09:15:10.339159","2016-04-18 09:15:10.339159" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchSuggestion < ActiveRecord::Base | |
attr_accessor :popularity, :term | |
def self.terms_for(prefix) | |
suggestions = where("term like ?", "#{prefix}_%") | |
suggestions.order("popularity desc").limit(10).pluck(:term) | |
end | |
def self.index_charts | |
ChartsController::Chart.find_each do |chart| | |
index_term(chart.title) | |
chart.title.split.each { |t| index_term(t) } | |
end | |
end | |
def self.index_term(term) | |
where(term: term.downcase).first_or_initialize do |suggestion| | |
suggestion.increment! :popularity | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment