Skip to content

Instantly share code, notes, and snippets.

@ruby-fu-ninja
ruby-fu-ninja / set_schema_migrations.rb
Last active August 29, 2015 14:25 — forked from n0ts/set_schema_migrations.rb
Set all migrations as up in schema_migrations table
Dir["db/migrate/*"].each do |v|
next if v == "." or v == ".." or v == ".svn"
version = File.basename(v).split('_').first
begin
ActiveRecord::Base.connection.execute("INSERT INTO schema_migrations values ('#{version}')")
rescue => e
p e
exit
end
end
@ruby-fu-ninja
ruby-fu-ninja / gist:89937db32999742aeb9f
Created May 16, 2014 09:01
Log query line number in Rails console
module QueryTrace
def self.enable!
::ActiveRecord::LogSubscriber.send(:include, self)
end
def self.append_features(klass)
super
klass.class_eval do
unless method_defined?(:log_info_without_trace)
alias_method :log_info_without_trace, :sql
@ruby-fu-ninja
ruby-fu-ninja / gist:6100740
Created July 28, 2013 23:39
Genereate httpasswd without apache tools
root@li615-48:~# echo -e "your-username:`perl -le 'print crypt("your-password","salt")'`" > /path/to/httpasswd
@ruby-fu-ninja
ruby-fu-ninja / serialized_attr_accessor.rb
Created January 17, 2013 07:19
Create attribute accessor's for serialized attributes when using the serialize method in Rails
module SerializedAttrAccessor
module ClassMethods
def define_serialized_attr_accessor(attribute)
self.class_eval %Q{
def #{attribute}=(val)
self.serialized_column[:#{attribute}] = val
end
@ruby-fu-ninja
ruby-fu-ninja / update_postgres_id_sequences_in_rails_models.rb
Last active November 20, 2023 23:18
Update Postgres id sequences on all of your Rails models
Dir[Rails.root.join('app', 'models', '*')].each do |path|
file = File.basename(path, '.rb')
begin
klass = file.classify.constantize
if klass.respond_to?(:select)
max_id = klass.maximum(:id).to_i + 1
sequence = ActiveRecord::Base.connection.execute("SELECT nextval('#{klass.table_name}_id_seq')").first['nextval'].to_i
if sequence <= max_id
puts "Updating sequence for #{klass.table_name}"
@ruby-fu-ninja
ruby-fu-ninja / gist:1696989
Created January 29, 2012 03:25
Extract controller and action from rails log
# Get number of lines
grep "Processing" log/development.log | wc -l
# Replace LINE_NUMBER the minimum line number
grep "Processing" log/development.log | awk 'NR > LINE_NUMBER { print $3 }' | sort | uniq
@ruby-fu-ninja
ruby-fu-ninja / munin_ubuntu
Created September 28, 2011 07:53
Munin Ubuntu Install
sudo apt-get install munin munin-node -y
cd /munin/parent/dir
mkdir munin
sudo chown munin:munin munin
sudo vim /etc/munin/munin.conf
Add htmldir to conf:
htmldir /munin/parent/dir/munin