Skip to content

Instantly share code, notes, and snippets.

@danhixon
Created April 6, 2010 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danhixon/358089 to your computer and use it in GitHub Desktop.
Save danhixon/358089 to your computer and use it in GitHub Desktop.
class Auction
# is categorized once or twice
# with a primary_category_id and a secondary_category_id
# there is a Category class that acts_as_tree
# this scope will select duplicates if auction
# is categorized with a "brother" category because
# it has two categorizations included in the query
named_scope :in_category, lambda { |category_id|
{ :joins =>
sanitize_sql_array(["JOIN categories as subcategory
ON primary_category_id = subcategory.id
OR secondary_category_id = subcategory.id
JOIN categories as category
ON subcategory.parent_id = category.id
AND category.id = ?", category_id])
}
}
# texticle
index do
title
description
end
end
# to pull results I chain the search parameters.
# first I grab the auctions in the given category:
@chain = Auction.in_category(category_id)
# calling search will use overwrite :select option
# :select => "#{table_name}.*, ts_rank_cd((#{full_text_indexes.first.to_s}), to_tsquery(#{connection.quote(term)})) as rank"
# only search if a search value is not empty
@chain = @chain.search(text) if !text.to_s.empty?
@chain.all
# so... if I pass in :select=>"Distinct auctions.*" it will be
# overwritten or i could reverse the order and the distinct will
# be overwritten. The other complication is that the
# category_id is also an optional parameter.
# I'd love it if I could do something like this:
:select => options[:select] ? "DISTINCT " + options[:select] : "DISTINCT auctions.*"
# options is not defined - if i could check for a :select defined already...
# how can i access the hash with which i am merging? probably not going to happen.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment