Skip to content

Instantly share code, notes, and snippets.

View aderyabin's full-sized avatar
👨‍💻
building something new

Andrey Deryabin aderyabin

👨‍💻
building something new
View GitHub Profile
@aderyabin
aderyabin / gist:731631
Created December 7, 2010 09:52
polymorphic_select_tag
# polymorphic_select_tag @item, :owner, Post.all(:order=>'created_at DESC').collect{|b| #[b.title, [Post, b.id]]}
def polymorphic_select_tag(object, field, option_array=[], options={}, html_options={})
option_tags = "<option #{'selected="selected"' if (object.try(field) == nil)}/>" +
option_array.map do |tuple|
"<option value='#{tuple[1][0]}-#{tuple[1][1]}' #{'selected="selected"' if ((object.try(field).try(:id).to_s == tuple[1][1].to_s && object.try(field).try(:type).to_s == tuple[1][0].to_s) rescue false)}>#{tuple[0]}</option>"
end.to_s
prefix = options[:prefix] || ''
object_id = object.id
@aderyabin
aderyabin / remove_duplicates.sql
Created May 28, 2010 15:09
Remove duplicates from table
SHOW CREATE TABLE old_table;
INSERT INTO new_table SELECT DISTINCT * FROM old_table;
DROP TABLE old_table;
RENAME TABLE new_table TO old_table;
@aderyabin
aderyabin / gitignore
Created March 24, 2010 04:48
.gitignore
log/*.log
.DS_Store
public/cache/**/*
public/system/**/*
config/database.yml
config/*.sphinx.conf
config/s3_credentials.yml
*~
*.cache
*.pid
@aderyabin
aderyabin / gist:335138
Created March 17, 2010 11:27
generate random sequence
# generate random sequence
#SecureRandom also has methods for: base64, hex, bytes, random_number
#see: http://api.rubyonrails.org/classes/ActiveSupport/SecureRandom.html
require 'active_support/secure_random'
ActiveSupport::SecureRandom.hex(16)
# outputs: 6dc19d21e958e04645cddadb96e9c927