Skip to content

Instantly share code, notes, and snippets.

@bayendor
bayendor / sql_limit_return.erb
Last active December 25, 2015 21:39
Code to display the SQL query from an ActiveRecord Search & to limit number of results returned. From the ransack gem demo.
<p>SQL: <%= @users.to_sql %></p>
<h2>Your first #{search_limit} results</h2>
<table>
<thead>
<th><%= sort_link @search, :id, {}, :method => action_name == 'advanced_search' ? :post : :get %></th>
<th><%= sort_link @search, :first_name, {}, :method => action_name == 'advanced_search' ? :post : :get %></th>
# ...
</thead>
@bayendor
bayendor / gist:7380393
Created November 9, 2013 01:39
Csv import model logic
# controller
def proc_csv
import = Import.find(params[:id])
if import.load_csv
flash[:notice] = "woo"
redirect_to some_url
else
flash[:error] = "ohoh"
end
end
@bayendor
bayendor / gist:7385546
Last active December 27, 2015 20:39
Checkbox in ransack search
# model CLINIC has many TREATMENTS
<%= check_box_tag "q[treatments_id_eq_ALL][]", treatment.id, {} %>
= f.input :category_name_eq, :collection=>Estate::Category.all.map(&:name), :as=>:check_boxes
@bayendor
bayendor / gist:7385660
Created November 9, 2013 13:55
Ransack multiple selects
f.collection_select :some_id_in, Model.all, :id, :name, {}, {:multiple => true}
# I have to do the following to get the select field I want:
search_form_for @search do |f|
f.collection_select :level_in, Log.levels, :to_s, :to_s, {}, { size: Log.levels.length, multiple: true }
@bayendor
bayendor / gist:7999530
Created December 17, 2013 03:24
Always use :inverse_of to connect two models in a belongs_to/has_many relationship. When you have two models in a belongs_to/has_many relationship, you should always use the :inverse_of option to tell ActiveRecord that they're two sides of the same association. By setting this option, Rails can optimize object loading so forum and forum.posts[0]…
class Forum < ActiveRecord::Base
has_many :posts, :inverse_of => :forum
end
class Post < ActiveRecord::Base
belongs_to :forum, :inverse_of => :posts
end
@bayendor
bayendor / audit_info.rb
Created March 14, 2014 15:32
Migration Results Import & Update Code
class AuditInfo < ActiveRecord::Base
belongs_to :arc_info
def self.import(file)
begin
if file.path
CSV.foreach(file.path, headers: true, col_sep: "\t") do |row|
audit_info = AuditInfo.create! row.to_hash
arc_info = audit_info.arc_info
arc_info.update_attribute(:status, audit_info.computed_status)
@bayendor
bayendor / unused_index.sql
Last active August 29, 2015 14:01 — forked from jberkus/gist:6b1bcaf7724dfc2a54f3
Finding Unused Indexes Query
WITH table_scans as (
SELECT relid,
tables.idx_scan + tables.seq_scan as all_scans,
( tables.n_tup_ins + tables.n_tup_upd + tables.n_tup_del ) as writes,
pg_relation_size(relid) as table_size
FROM pg_stat_user_tables as tables
),
all_writes as (
SELECT sum(writes) as total_writes
FROM table_scans
@bayendor
bayendor / cleaner.rb
Last active August 29, 2015 14:07
Turing RegEx Cleaner
class Cleaner
def domain_from_email(email)
email.match(/([\w+\-.]+)@([a-z\d\-.]+\.[a-z]{2,4}+)/)[2]
end
def area_code_only(phone_number)
phone_number.match(/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/)[3]
end
def get_secret_message(string)
@bayendor
bayendor / into_to_sql.md
Last active August 29, 2015 14:08
Intro to SQL Notes

#What are database tables, column, and rows? What's the purpose of each?

Table is a collection of columns and rows

Column is a particular datatype, named with lowercase_separated_with_underscores

Row is a collection of data elements in a table each represented by a column, synonym for record

#How's a database similar and different from a spreadsheet?

@bayendor
bayendor / user_stories.rb
Created November 3, 2014 20:29
Intro to Capybara
describe "the idea creation process", :type => :feature do
it "goes to the home page" do
visit '/'
within("#session") do
fill_in 'Title', :with => 'great idea'
fill_in 'Description', :with => 'so awesome all the things'
end
click_button 'submit'
expect(page).to have_content 'Great Idea'
end