Skip to content

Instantly share code, notes, and snippets.

@DimaD
Created May 14, 2009 08:05
Show Gist options
  • Save DimaD/111559 to your computer and use it in GitHub Desktop.
Save DimaD/111559 to your computer and use it in GitHub Desktop.
class Project
include DataMapper::Resource
property :id, Serial
property :title, Text
property :abstract, Text
property :combined_tsvector, TSvector
#Generates CREATE FUNCTION insert_project
stored_procedure :insert do |f|
f.language :sql
f.return Void
f.arg :title, Text
f.arg :abstract, Text
f.body <<-SQL
INSERT INTO :table_name (title, abstract, combined_tsvector)
VALUES (:title, :abstract, setweight(to_tsvector(:title), 'A') || setweight(to_tsvector(:abstract), 'C'))
SQL
end
stored_procedure :combined_search do |f|
f.language :sql
f.return setof([Integer, Float])
f.arg :term, Text
f.body <<-SQL
SELECT id, ts_rank_cd(combined_tsvector, q) AS relevancy
FROM :table_name, to_tsquery(:term) AS q
ORDER BY relevancy DESC
SQL
end
end
Project::insert("Test project", "Test abstract with some words") #Calls SELECT * FROM insert_projects in DB
Project::combined_search("title")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment