How to use ActiveRecord without Rails
This is an example of how to use ActiveRecord without Rails:
1 ['/model', '/db'].each do |folder| 2 $:.unshift File.dirname(__FILE__) + folder 3 end 4 5 require 'test/unit' 6 require 'rubygems' 7 require 'activerecord' 8 9 ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log') 10 ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/config/database.yml')) 11 ActiveRecord::Base.establish_connection('sqlite3') 12 13 require 'schema' 14
Schema contains, for example:
1 ActiveRecord::Schema.define :version => 0 do 2 create_table :languages, :force => true do |t| 3 t.string :name 4 end 5 end
How to create a daemon process using Ruby and the daemons RubyGem
This snippets shows you how to create a daemon process out of an ordinary Ruby script.
First you’ll need the daemons gem:
1 gem install daemons
Then you’ll need the daemon script, for example daemon.rb:
1 require 'rubygems' 2 require 'daemons' 3 4 pwd = File.dirname(File.expand_path(__FILE__)) 5 file = pwd + '/../lib/background_service.rb' 6 7 Daemons.run_proc( 8 'background_service', # name of daemon 9 # :dir_mode => :normal 10 # :dir => File.join(pwd, 'tmp/pids'), # directory where pid file will be stored 11 # :backtrace => true, 12 # :monitor => true, 13 :log_output => true 14 ) do 15 exec "ruby #{file}" 16 end
Change the file variable to point to the script you want to daemonize and your good to go.
You can now execute the daemon.rb script without parameters to get a list of available commands for controlling the daemon process:
1 ERROR: no command given 2 3 Usage: lib/background_service.rb <command> <options> -- <application options> 4 5 * where <command> is one of: 6 start start an instance of the application 7 stop stop all instances of the application 8 restart stop all instances and restart them afterwards 9 run start the application and stay on top 10 zap set the application to a stopped state 11 12 * and where <options> may contain several of the following: 13 14 -t, --ontop Stay on top (does not daemonize) 15 -f, --force Force operation 16 17 Common options: 18 -h, --help Show this message 19 --version Show version
How to use named_scope in Rails
Simple example of how to use the named_scope feature:
1 class Feed < ActiveRecord::Base 2 3 named_scope :active, :conditions => "(active = 1)" 4 named_scope :stale, :conditions => ["last_updated > ?", 30.minutes.ago.to_s(:db)]
Usage:
1 Feed.active # return the active feeds
Chaining is also possible:
1 Feed.active.stale # return the feeds that need to be updated
Sample thinking-sphinx configuration
Sorting an array according to a specific language using Gibberish.
You can use Ruby’s Enumerable sort_by to sort an array correctly using Gibberish and Gibberish DB
It’s not pretty, but it works on non-paginated lists. For pagination something else must bee cooked up. Here I sort an array of Category objects according to the localized name of the category.
1 @categories = @categories.sort_by{|a| 2 a[:name][].to_s 3 }