Skip to content

Instantly share code, notes, and snippets.

@maxivak
maxivak / gist:3924877
Created October 20, 2012 21:29
Django: raw SQL - SELECT
# file libs/dbutil.py
from itertools import *
from django.db import connection
def query_to_dicts(query_string, *query_args):
cursor = connection.cursor()
cursor.execute(query_string, query_args)
col_names = [desc[0] for desc in cursor.description]
while True:
@maxivak
maxivak / gist:3924899
Created October 20, 2012 21:36
Django: raw SQL - UPDATE
from django.db import connection, transaction
cursor = connection.cursor()
cursor.execute("UPDATE yourtablename SET field1=value1, .. WHERE .. ")
transaction.set_dirty()
transaction.commit()
@maxivak
maxivak / resize_nocrop_noscale
Last active April 25, 2023 03:27
Crop and resize an image using MiniMagick in Ruby on Rails. Two approaches.
def resize_nocrop_noscale(image, w,h)
w_original = image[:width].to_f
h_original = image[:height].to_f
if w_original < w && h_original < h
return image
end
# resize
image.resize("#{w}x#{h}")
@maxivak
maxivak / gist:3924988
Created October 20, 2012 22:02
Dynamic connection to MySQL database in Ruby on Rails (mysql2)
require 'mysql2'
db = Mysql2::Client.new(:host => 'localhost', :username => 'root', :password=> 'password', :database => 'db_name' )
res = db.query("select * from users")
res.each do |row|
col1 = row['id']
col2 = row['email']
..
@maxivak
maxivak / send_data-send_file-remote-images-download
Created January 1, 2013 23:34
Rails. Download remote image as attachment in browser
# in controller
# for local files
send_file '/path/to/file', :type => 'image/jpeg', :disposition => 'attachment'
# for remote files
require 'open-uri'
url = 'http://someserver.com/path/../filename.jpg'
data = open(url).read
send_data data, :disposition => 'attachment', :filename=>"photo.jpg"
@maxivak
maxivak / rails-db-ids-list
Created January 6, 2013 05:36
Get a list of IDs from database
# 1. connection.select_values or connection.select_all
ids = ActiveRecord::Base.connection.select_values("select id from users WHERE ...")
# array of integers
# ["1", "2", "5", "6", "7", "8", "3", "10", "11", "9"]
# return hashes without model
sql = "sql query"
rows = User.connection.select_all(sql.squish!)
@maxivak
maxivak / rails-ids-list
Created January 6, 2013 05:44
Get a list of IDs from database
# 1. connection.select_values or connection.select_all
ids = ActiveRecord::Base.connection.select_values("select id from users WHERE ...")
# array of integers
# ["1", "2", "5", "6", "7", "8", "3", "10", "11", "9"]
# return hashes without model
sql = "sql query"
rows = User.connection.select_all(sql.squish!)
@maxivak
maxivak / rails-assets-cdn.md
Last active May 24, 2017 11:30
Rails assets and CDN

Rails assets CDN

# serve all assets from CDN  server
config.action_controller.asset_host = 'http://cdn.mydomain.com'

#or if your files in a folder on CDN server
config.action_controller.asset_host = 'http://cdn.mydomain.com/myfolder'

@maxivak
maxivak / 000-readme.md
Last active December 15, 2015 10:49
Rails custom error pages

Rails custom error pages

@maxivak
maxivak / passenger_memory_stats
Last active December 21, 2015 20:58
Munin Passenger plugin
#!/usr/bin/env ruby
# put in /etc/munin/plugins and restart munin-node
# by Dan Manges, http://www.dcmanges.com/blog/rails-application-visualization-with-munin
# NOTE: you might need to add munin to allow passwordless sudo for passenger-memory-stats
def output_config
puts <<-END
graph_category App
graph_title Passenger memory stats
graph_vlabel count