Skip to content

Instantly share code, notes, and snippets.

@vakiliy
Last active October 4, 2015 08:49
Show Gist options
  • Save vakiliy/f6016b32347a08d8d9bf to your computer and use it in GitHub Desktop.
Save vakiliy/f6016b32347a08d8d9bf to your computer and use it in GitHub Desktop.
# Arel
#"ROW_NUMBER() OVER ()"
row_nubmer = Arel::Nodes::NamedFunction.new('ROW_NUMBER', [] )
sql = Arel::Nodes::Over.new(row_number).as('row_number')
row_nubmer.over.as('row_number')
table = Arel::Table.new :users
# COUNT("users"."id") OVER () AS foo
table[:id].count.over.as('foo')
# array_agg
func = ->(){ Arel::Nodes::NamedFunction.new('array_agg', [self]) }
Arel::Attributes::Attribute.send :define_method, :array_agg, func
Loan.group(:state).calculate(:array_agg, :id)
# SELECT array_agg("loans"."id") AS array_agg_id, state AS state FROM "loans" GROUP BY "loans"."state"
# Phone Validation
validates :phone, :format => {
:with => /(\+7)|8(\s){0,1}(\d){3,3}(\-|\_\s){0,1}(\d){3,3}(\-|\_\s){0,1}(\d){2,2}(\-|\_\s){0,1}(\d){2}/
}
# application
rescue_from(ActiveRecord::RecordNotFound, :with => :record_not_found)
rescue_from(ActionController::RoutingError, :with => :path_not_found)
rescue_from(RoleBase::AccessDenied, :with => :record_not_found)
def record_not_found; end
def path_not_found; end
# put this in config/initializer/fields_with_errors.rb
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if html_tag =~ /<(input|label|textarea|select)/
%{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label> </div>}.html_safe
else
html_tag
end
end
# Hstore
# Find all products that have a key of 'author' in data
Product.where("data ? :key", :key => 'author')
# Find all products that have a 'pages' and '368' key value pair in data
Product.where("data @> (:key => :value)", :key => 'pages', :value => '368')
# Find all products that don't have a key value pair 'pages' and '999' in data
Product.where("not data @> (:key => :value)", :key => 'pages', :value => '999')
# Find all products having key 'author' and value like 'ba' in data
Product.where("data -> :key iLIKE :value", :key => 'author', :value => "%KitKat%")
# Expiration
response.headers["Expires"] = 1.year.from_now.httpdate
# Gem commands
require 'rubygems'
gem = Gem::GemRunner.new
gem.run(['help','commands'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment