Skip to content

Instantly share code, notes, and snippets.

@dncrht
Last active December 20, 2015 08:09
Show Gist options
  • Save dncrht/6098764 to your computer and use it in GitHub Desktop.
Save dncrht/6098764 to your computer and use it in GitHub Desktop.

20. Use Full Text Search in PostgreSQL

$ rails g resource article subject body:text
class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
      t.string :subject
      t.text   :body
      t.column :search, "tsvector"
      
      t.timestamps
    end
    
    execute <<- END_SQL
      CREATE INDEX articles_search_index ON articles USING gin(search);
      
      CREATE TRIGGER articles_search_update
      BEFORE INSERT OR UPDATE ON articles
      FOR EACH ROW EXECUTE PROCEDURE
        tsvector_update_trigger( search, 'pg_catalog.english', subhect,body );
    
    END_SQL
  end
end
class Article < ActiveRecord::Base
  attr_accessible :body, :subject
  
  def self.search(query)
    sql = sanitize_sql_array(["plainto_tsquery('english', ? )", query])
    where(
      "seearch @@ #{sql}"
    ).order(
      "ts_rank_cd(search, #{sql}) DESC"
    )
  end
end
$ rails c
Loading development environment (Rails 3.2.3)
>> Article.create!(subject: "Full Text Search")
=> #<Article id 1, …>
>> Article.create!(body: "A stemmed search.")
=> #<Article id 2, …>
>> Article.create!(body: "You won't find me!")
> #<Article id 3, …>
>> Article.search("search").map { |a| a.subject || a.body }
=> ["Full Text Search", "A stemmed search."]
>> Article.search("stemming").map { |a| a.subject || a.body }
=> "A stemmed search."

32. Use Blocks to Avoid Assignments in Views

<table>
  <% @cart.products.each do |product| %>
    <tr>
      <td> <%= product.name %> </td>
      <td> <%= number_to_currency product.price %> </td>
    </tr>
  <% end %>
    <tr>
      <td> Subtotal </td>
      <td>  <%= number_to_currency @cart.total %> </td>
    </tr>
    <tr>
      <td> Tax </td>
      <td> <%= number_to_currency (tax = calculate_tax(@cart.tototal)) %> </td>
    </tr>
    <tr>
      <td> Total </td>
      <td>  <%= number_to_currency(@cart.total + tax %> </td>
    </tr>
</table>

=>

<table>
  <% @cart.products.each do |product| %>
    <tr>
      <td> <%= product.name %> </td>
      <td> <%= number_to_currency product.price %> </td>
    </tr>
  <% end %>
    <tr>
      <td> Subtotal </td>
      <td>  <%= number_to_currency @cart.total %> </td>
    </tr>
    <% calculate_tax @cart.total do |tax| %>
      <tr>
        <td> Tax </td>
        <td> <%= number_to_currency tax) %> </td>
      </tr>
      <tr>
        <td> Total </td>
        <td>  <%= number_to_currency(@cart.total + tax %> </td>
      </tr>
    <% end %>
</table>
module CartHelper
  def calculat_text(total, user = current_user)
    tax = TaxTable.for(user).calculate(total)
    if block_given?
      yield tax
    else
      tax
    end  
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment