You are currently browsing the category archive for the ‘Rails’ category.

Deletes taking to long, just take a break :)
(so replication can catch up/things get unblocked)

Only use save sql, there is no escapeing.

  class ActiveRecord::Base
    def self.slow_delete_all(condition)
      count = 0
      loop do
        result = ActiveRecord::Base.connection.send(:delete_sql, "delete from #{table_name} where #{condition} limit 10000")
        count += result
        break if result == 0
        sleep 1
      end
      count
    end
  end

If your has_and_belongs_to_many associations suddenly need to know more, its time to convert them to a real model.

  • rename table
  • add id
  • add created_at/updated_at as not-null (need to set existing records to something)

Code

class CreateJoinModel < ActiveRecord::Migration
  def change
    rename_table :products_users, :purchases
    add_column :purchases, :id, :primary_key
    [:created_at, :updated_at].each do |column|
      add_column :purchases, column, :timestamp, null: false, default: Time.at(0)
      change_column_default :purchases, column, nil
    end
  end
end

require ‘resque/tasks’
require ‘resque_scheduler/tasks’
Scheduler needs very little cpu, just start it with a worker.

desc "schedule and work, so we only need 1 dyno"
task :schedule_and_work do
  if Process.fork
    sh "rake environment resque:work"
  else
    sh "rake resque:scheduler"
    Process.wait
  end
end

Make your rspec output look more interesting!
(yes we had too much spare time at our hands… :D )

# spec/spec_helper.rb
# encoding: UTF-8

if defined? RSpec::Core::Formatters::ProgressFormatter
  RSpec::Core::Formatters::ProgressFormatter.class_eval do
    DOTS = ['☘','⚘']
    def example_passed(example)
      output.print green(DOTS[rand(DOTS.size)])
    end
  end
end
# in your controller ...
BLANK_GIF = Base64.decode64("R0lGODlhAQABAPAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

render text: BLANK_GIF, type: 'image/gif'
Follow

Get every new post delivered to your Inbox.

Join 76 other followers