Skip to content

Instantly share code, notes, and snippets.

squirrel_staging=> explain analyze select distinct on (bet_id) bet_id, sport_id from sports_bet_selections sbs ;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Unique (cost=0.00..3000.21 rows=62774 width=8) (actual time=0.121..245.682 rows=65640 loops=1)
-> Index Scan using index_sports_bet_selections_on_bet_id on sports_bet_selections sbs (cost=0.00..2827.03 rows=69274 width=8) (actual time=0.117..99.272 rows=69624 loops=1)
Total runtime: 308.829 ms
(3 rows)
squirrel_staging=> explain analyze select bet_id from sports_bet_selections sbs where sbs.id = (select max(id) from sports_bet_selections where bet_id = sbs.bet_id);
QUERY PLAN
#app/decorators/application.rb
class ApplicationDecorator < Draper::Decorator
def link_to_edit
if h.can? :edit, object
h.link_to 'Edytuj', h.public_send("edit_#{object.class.name.underscore}_path", object)
end
end
def link_to_destroy
@sufleR
sufleR / gplus_api.rb
Last active August 29, 2015 14:10
google plus configuration
class GplusApi
cattr_reader :plus, :key, :token_expiration
attr_reader :client
private :key
def initialize(user_id)
client_set_up
@user_id = user_id
end
@sufleR
sufleR / pqrorwp_console.rb
Created November 5, 2014 18:25
Persistent queries in Ruby on Rails with PostgreSQL
> search = AdvertSearch.new(brand: 'Honda', production_year_gte: 2000, production_year_lte: 2004, last_week: true)
> search.results.to_sql
=> "SELECT \"adverts\".*"
"FROM \"adverts\""
"WHERE \"adverts\".\"brand\" = 'Honda'"
"AND (`adverts`.`production_year` >= 2000)"
"AND (`adverts`.`production_year` <= 2004)"
"AND (\"adverts\".\"published_at\" BETWEEN '2014-10-28 00:00:00.000000' AND '2014-11-04 00:00:00.000000')"
> squery = SavedQuery.create!(name: 'honda for my friend', advert_search: search)
@sufleR
sufleR / pqrorwp_saved_query.rb
Created November 5, 2014 18:19
Persistent queries in Ruby on Rails with PostgreSQL
#app/models/saved_query.rb
class SavedQuery < ActiveRecord::Base
embeds_one :advert_search, column_name: :query_params
end
@sufleR
sufleR / pqrorwp_advert_search.rb
Created November 5, 2014 18:18
Persistent queries in Ruby on Rails with PostgreSQL
#app/searches/advert_search.rb
class AdvertSearch < Searchlight::Search
extend Embedson::Model
embedded_in :saved_query
search_on Advert.all
searches :brand, :production_year_gte, :production_year_lte, :last_week
@sufleR
sufleR / pqrorwp_shell.sh
Created November 5, 2014 18:14
Persistent queries in Ruby on Rails with PostgreSQL
> rails generate model saved_query name:text query_params:json
> rake db:migrate
@sufleR
sufleR / pqrorwp_query.rb
Last active August 29, 2015 14:08
Persistent queries in Ruby on Rails with PostgreSQL
Advert.where( brand: ‘Honda’,
production_year: 2000..2004,
published_at: (Time.current - 1.week).at_beginning_of_day..Time.current.at_beginning_of_day
)
@sufleR
sufleR / item.rb
Last active August 29, 2015 14:08
controller to share items for social media
# og_hash looks like this
# og_title and og_type are calculated
# also image_url
def og_hash
og_h = {
title: og_title,
type: og_type,
description: text
}
og_h.merge(image: image_url) if image_url.present?
@sufleR
sufleR / console.sh
Last active August 29, 2015 14:07
Example of embeding model in ActiveRecord using JSON and postgreSQL
2.1.1 :052 > t = Test.new()
=> #<Test id: nil, data: nil, created_at: nil, updated_at: nil>
2.1.1 :053 > t.virt = Virt.new(address: { street: 'pięknie jest', nr: 5 })
=> #<Virt:0x00000106100458 @address={:street=>"pięknie jest", :nr=>5}, @name=nil, @children=[]>
2.1.1 :054 > t.save!
(0.4ms) BEGIN
SQL (0.4ms) INSERT INTO "tests" ("created_at", "data", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["created_at", "2014-10-07 21:56:49.991978"], ["data", "{\"name\":null,\"address\":{\"street\":\"pięknie jest\",\"nr\":5},\"children\":[]}"], ["updated_at", "2014-10-07 21:56:49.991978"]]
(2.0ms) COMMIT
=> true
2.1.1 :055 > t.reload