Created
November 25, 2011 05:18
-
-
Save mtcmorris/1392860 to your computer and use it in GitHub Desktop.
DM AR diff
This file contains hidden or 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
@@ -138,29 +138,12 @@ class Content < ActiveRecord::Base | |
) | |
end | |
- # DataMapper made me do it, sorry baby Jesus. | |
- def self.search(keywords, options = {}) | |
- options = { | |
- conditions: ["true"] | |
- }.merge(options) | |
- | |
- current_query = query.merge(options) | |
- | |
- columns_sql = repository.adapter.send(:columns_statement, current_query.fields, false) | |
- conditions_sql, conditions_values = repository.adapter.send(:conditions_statement, current_query.conditions, false) | |
- order_sql = repository.adapter.send(:order_statement, current_query.order, false) | |
- limit_sql = current_query.limit || 50 | |
- | |
- bind_values = [keywords] + conditions_values | |
- | |
- find_by_sql([<<-SQL, *bind_values]) | |
- SELECT #{columns_sql}, ts_rank_cd(search_vector, query) AS rank | |
- FROM content | |
- CROSS JOIN plainto_tsquery(?) query | |
- WHERE #{conditions_sql} AND (query @@ search_vector) | |
- ORDER BY rank DESC, #{order_sql} | |
- LIMIT #{limit_sql} | |
- SQL | |
+ # Performs a full-text search on the given keywords. | |
+ def self.search(keywords) | |
+ select("content.*, ts_rank_cd(search_vector, query) AS rank") | |
+ .joins(sanitize_sql_array(["CROSS JOIN plainto_tsquery(?) query", keywords])) | |
+ .where("query @@ search_vector") | |
+ .order("rank DESC") | |
end | |
def related |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment