ActiveRecord reconnect to database
ActiveRecord::Base.connection.reconnect!
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)
Jamie Wilkinson http://tramchase.com
ActiveRecord::Base.connection.reconnect!
# 1) make yrself a user and stop being root # 2) main web stack + ruby sudo aptitude install -y make screen sudo aptitude install -y apache2 php5 mysql-server sudo aptitude install -y subversion postfix sudo aptitude install -y ruby1.8 ruby1.8-dev rubygems irb sudo aptitude install -y imagemagick librmagick-ruby1.8 librmagick-ruby-doc libfreetype6-dev xml-core # 3) gemz # rubygems appears to be broken as hell. # gem update --system does squat # had to manually d/l rubygems 1.0.1, ruby setup.rb wget http://rubyforge.org/frs/download.php/29548/rubygems-1.0.1.tgz tar xzvf rubygems-1.0.1.tgz cd rubygems-1.0.1 sudo ruby setup.rb sudo gem install rails mongrel merb map_by_method
jQuery.ajaxSetup({ ‘beforeSend’: function(xhr) {xhr.setRequestHeader(“Accept”, “text/javascript”)} })
class Something < ActiveRecord::Base # Validations validate :validate_state_change # State Machine States acts_as_state_machine :initial => :new state :new state :enabled, :after => :after_enabled state :disabled, :after => :after_disabled # State Machine Events event :enabled do transitions :from => :new, :to => :enabled transitions :from => :disabled, :to => :enabled end event :disabled do transitions :from => :new, :to => :disabled transitions :from => :enabled, :to => :disabled end # Instance Methods def validate_state_change return if new_record? old = self.class.find(id) old_state = old.state new_state = self.state self.state = old_state if old_state != new_state begin if self.method("#{new_state}!").call != true errors.add(:state, "cannot transition from #{old_state} to #{new_state}") end rescue NameError end self.state = new_state end end end
# load production data, still needs some polish # based on code from http://push.cx/2007/capistrano-task-to-load-production-data # cap 2.0 compatible # exec is being weird, had to end w/ a syscall :\ any ideas? desc "Load production data into development database" task :load_production_data, :roles => :db, :only => { :primary => true } do require 'yaml' ['config/database.yml'].each do |file| database = YAML::load_file(file) filename = "dump.#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.sql.gz" # on_rollback { delete "/tmp/#{filename}" } # run "mysqldump -u #{database['production']['username']} --password=#{database['production']['password']} #{database['production']['database']} > /tmp/#{filename}" do |channel, stream, data| run "mysqldump -h #{database['production']['host']} -u #{database['production']['username']} --password=#{database['production']['password']} #{database['production']['database']} | gzip > /tmp/#{filename}" do |channel, stream, data| puts data end get "/tmp/#{filename}", filename # exec "/tmp/#{filename}" password = database['development']['password'].nil? ? '' : "--password=#{database['development']['password']}" # FIXME pass shows up in process list, do not use in shared hosting!!! Use a .my.cnf instead # FIXME exec and run w/ localhost as host not working :\ # exec "mysql -u #{database['development']['username']} #{password} #{database['development']['database']} < #{filename}; rm -f #{filename}" `gunzip -c #{filename} | mysql -u #{database['development']['username']} #{password} #{database['development']['database']} && rm -f gunzip #{filename}` end end
def render(*args) args.first[:layout] = false if request.xhr? and args.first[:layout].nil? super end
def ajax_request? request.env['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' || params[:ajax] # params[:ajax] for safe-keeping, FIXME if it works w/ all browsers end