Archive for the 'Rails' Category

EventMachine Asynchronous HTTP Client – Custom Headers (User-Agent)

December 16th, 2010 | Category: Development,Rails

If you have a need to set custom request headers like User-Agent or Host, here is how you do so via the EM client

http = EventMachine::HttpRequest.new(url)
  .get(:head => {"User-Agent" => "Custom Agent"})
No comments

Apache Phusion Passenger: Apache 2 not found

December 06th, 2010 | Category: Development,Linux,Rails

This may be pretty basic, but figured it may be useful to others. According to the passenger docs, it will look for Apache in the $PATH environment variable. So to fix the Apache 2 not found error, you first need to find the install location. A very helpful command is “whereis” that comes standard on most *NIX systems. If you have whereis installed copy/paste the following into the command line

APACHE_PATH=`whereis apache | awk '{ print $2 }'`

That will set APACHE_PATH to the path returned from whereis. Before you continue, please make sure the path was set properly.

echo $APACHE_PATH

If everything looks good, update the $PATH environment variable.

export PATH=$APACHE_PATH/bin:$PATH

Then check path to ensure it was updated

echo $PATH
No comments

Handy ActiveRecord Migration Methods

April 23rd, 2010 | Category: Development,Rails,Uncategorized

We are undergoing a massive DB schema redesign. Much of the work is just tightening up the schema through optimizing indexes, setting default boolean and integer fields, and updating NULL allowed fields. Here is a little module I wrote to save massive amounts of code.

module MigrationHelper
  def index_exists?(table, lookup)
    lookup = Hash[:columns, Array(lookup)] unless lookup.is_a?(Hash)
    lookup.symbolize_keys!
    lookup.each{|key,value|lookup[key]=(value.is_a?(Array))?(value.collect{|v|v.to_s}):(value.to_s)}
    indexes(table).collect(&lookup.keys.first).include?(lookup.values.first)
  end
 
  def change_column_nulls(table_name, nullable, *column_names)
    column_names.each do |column_name|
      table_columns = columns(table_name) 
      column_def = table_columns.select{|c|c.name == column_name.to_s}
      raise "Unknown column name #{column_name} for #{table_name}\nAvailable columns: #{table_columns.collect(&:name).to_sentence}" if column_def.empty?
      change_column table_name, column_name, column_def.first.type, :null => nullable
    end
  end
 
  def change_column_defaults(table_name, default, *column_names)
    column_names.each do |column_name|
      change_column_default table_name, column_name, default
    end
  end
end

I fond it necessary to have the index_exists? method mainly for my development purposes. When I’m optimizing tables by doing complex indexes, I often do them right on the DB first before throwing them into a migration. By having this check I can easily skip over it if it already exists. It takes the same options as add_index. You can pass table and column names, or just table and index name. Here are some usage examples:

add_index :table, :column unless index_exists?(:table, :column)
add_index :table, [:column1, :column2] unless index_exists?(:table, [:column1, :column2])
add_index :table, [:column1, :column2, :column3], :name => "some_index_name" unless index_exists?(:table, :name => "some_index_name")

The method change_column_nulls is used to mass change the NULL allowed columns for a given table. Really not much to it, but saves a lot of code over doing change_table or change_column. Here is the usage:

change_column_nulls :table, false, :column1, :column2

And lastly change_column_defaults is just a wrapper around change_column_default for mass assignment. Usage is the same as change_column_nulls passing the default values as the second argument. I thought about combining the two, but it didn’t offer very much savings.

These were tested using MySql 5. I’m not sure about the compatibility with other servers.

No comments

Rails ActiveRecord MySql varbinary

March 26th, 2010 | Category: Development,Rails

Although ActiveRecord has the following warning about database specific column types: “You may use a type not in this list as long as it is supported by your database (for example, “polygon” in MySQL), but this will not be database agnostic and should usually be avoided. ActiveRecord” You may have optimizations specific to a database server like MySql’s varbinary. Here is how to create the a table with a varbinary(1000) column.

create_table :page_error_sources do |t|
  t.column :source, 'varbinary(1000)', :null => false
  t.timestamps
end
No comments

Aggregation with MySQL and ActiveRecord

July 31st, 2009 | Category: Development,Rails

I had some data that I wanted to aggregate by year for a flash graphing app called amCharts. Here was the SQL query I used to gather the data:

SELECT AVG(column1) AS column1, AVG(column2) AS column2 FROM table1 t1
JOIN table2 t2 ON t2.id = t1.tabel2_id
GROUP BY YEAR(t2.datetime_field)

Rather than using this sql in ActiveRecord via find_by_sql, I figured I’d make it pretty and use the built in find method. Here is what I came up with:

Table1.find(:all, :select => 't2_id, AVG(column1) as column1, AVG(column2) as column2', :joins => :t2, :group => 'YEAR(t2.datetime_field)')

The :select option allows you to define the attributes you want returned with the AR object. I’m using MySQL to do the averaging of the data via the AVG function. I would normally use the :include option for joins, but I couldn’t get it to work with this query, so the :joins option seemed to work well by passing it the association definition from the model. The :group option simply works the same as the SQL group by. Just pass it a sql fragment, in my case just the year field portion of the datetime field.

No comments

Passing around arguments in rake tasks

April 03rd, 2009 | Category: Development,Rails

So I had the need to pass arguments from one rake task to another.  This is easy to do from the command line, but gets a bit tricking when its being called from within another rake task.  So here is the task that is being called:

  1 namespace :database do
  2   desc %{Dump DB to create restore data in case of error}
  3   task :dump, :tables, :needs  => [:setup] do |t, args|
  4     filename = "#{DB_NAME}#{'-'+args.tables.values.join('-') unless args.tables.nil?}-#{DB_DUMP_TIME}.sql.gz"
  5     puts "Dumping the database to '#{filename}'"
  6 
  7     cmd = ""
  8     cmd << "echo 'SET AUTOCOMMIT=0;'|gzip> #{filename};"
  9     cmd << "echo 'SET FOREIGN_KEY_CHECKS=0;'|gzip>> #{filename};"
 10     cmd << "mysqldump -u #{DB_USER} -p'#{DB_PASSWORD}' "
 11     cmd << "-h #{DB_HOST} #{DB_NAME} "
 12     cmd << "--tables #{args.tables.values.join(' ')} " unless args.tables.nil?
 13     cmd << "|gzip>> #{filename};"
 14     cmd << "echo 'SET FOREIGN_KEY_CHECKS=1;'|gzip>> #{filename};"
 15     cmd << "echo 'COMMIT;'|gzip>> #{filename};"
 16     cmd << "echo 'SET AUTOCOMMIT=1;'|gzip>> #{filename};"
 17 
 18     if system(cmd)
 19       puts "Successfully ran databse:dump to restore run:"
 20       puts "rake database:reload DB_DUMP_TIME='#{DB_DUMP_TIME}'"
 21     else
 22       puts "rake database:dump was unsuccessful"
 23     end
 24   end

So this task is used to dump the database for our app. I added the ability to pass in an optional hash to allow it to only dump the specified tables. Here are the important lines to note.

task :dump, :tables, :needs  => [:setup] do |t, args|

The “tables” argument specifies what arguments are being passed in. The “t” and “args” block defines the task and the arguments. Your “tables” hash is now available through “args” values.

Now when I call this task from within another rake task, I can do something like this.

tables = {:table1 => 'users', :table2 => 'roles'}
Rake::Task["database:dump"].invoke(tables)

Note, I define the hash outside of the invoke because it is used by two other tasks, but it could be directly passed to the task as an argument.

No comments

Rails, Time Zones, and Scheduling

November 06th, 2008 | Category: Development,Rails

The time zone support in rails has vastly improved since the integrated TZInfo into Rails 2.1. However if you have any scheduled tasks stored in your DB, you’ll notice it quickly breaks down. Here is a short and sweet explanation:
I have an app that delivers system notification to users at a specified time they set. I have a field in my “reminders” table called “deliver_at” which is a datetime field. The data is stored as a UTC timestamp. Everything works as expected until daylight savings time, or the user changes their time zone. This is because the “deliver_at” timestamp has no context of what offset was used when the record was created. So when daylight saving rolls around your messages are now delivered an hour off. Or if the user moves to a new timezone, it will be off by x hours. Here is the solution I came up with.
I added a field to the “reminders” table called “offset”.

add_column :reminders, :offset, :decimal, :precision =>; 4, :scale => 2, :null =>; false, :default => 0.0

This field is recorded when the record is first created. You can override the attribute using write_attribute and read_attribute, but I was already doing a seporate update call, so I just stuck it there. *Important, make sure you are setting the Time Zone on login.

self.update_attributes!(:delivered_at => Time.zone.now, :next_reminder_at => date, :offset => user_utc_offset)

Here is my user_utc_offset method:

def user_utc_offset
    TimeZone[self.user.timezone].utc_offset.to_f / 1.hour.to_f
end

I’m using floats because not all the offsets are whole numbers. Same reason I used a decimal field in the DB. Then when I calculate my next reminder delivery, I basically do this:

offset_adjustment = adjust_for_difference_in_offsets
		  case self.reminder.repeat
		      when "daily"
			  next_reminder = reminder.deliver_at + 1.day - offset_adjustment.hours
		      when "weekly"
			  next_reminder = reminder.deliver_at + 7.days - offset_adjustment.hours
		      when "monthly"
			  next_reminder = reminder.deliver_at + 1.month - offset_adjustment.hours
		   end

I have a case statement to calculate daily, weekly, and monthly reminders. Here is the method to calculate the offset difference.

def adjust_for_difference_in_offsets
    user_utc_offset - self.offset.to_f
  end

I use the above for the mailer, but we need to do the same for displaying the datetime stamp to the user. So I created a helper method that does like so:

def reminder_repeat_datetime(reminder)
    offset_adjustment = reminder.adjust_for_difference_in_offsets
    case reminder.repeat
      when "monthly"
        repeat = "#{reminder.deliver_at.strftime('%d')}th"
      when "weekly"
        repeat = "#{reminder.deliver_at.strftime('%A')}s"
      when "daily"
        repeat = "Daily"
      when "once"
        repeat = "#{reminder.deliver_at.strftime('%B %d, %Y')}"
    end
    "#{repeat} at #{(reminder.deliver_at - offset_adjustment.hours).strftime('%I:%M %p')}"
  end

A lot of the above is just pretty formatting of the datetime for the user. You could most likely just get away with something like the last line. That is called from my index view in my reminders controller like so:

<%= reminder_repeat_datetime(reminder) %>

So now no matter if the offset changes because of DST or the user changes their timezone, the offset will be calculated and applied to the datetime field.

No comments

Gem cache search depricated in boot.rb

October 23rd, 2008 | Category: Development,Rails

I was sick of getting the following deprication warning running rubygems version 1.3.0 and a rails 1.2.6 project.

“Gem::SourceIndex#search support for String patterns is deprecated
./script/../config/boot.rb:20 is outdated”

So I made some modifications to the boot.rb file so that it checks for the rails gem similar to the rails 2.x apps.  The gem itself is still loaded in the 1.2 fashoin.  Here is a diff on the changes

20,29c20,25
<       rails_gem = Gem.cache.search('rails', "=#{rails_gem_version}.0").sort_by { |g| g.version.version }.last
< 
<       if rails_gem
<         gem "rails", "=#{rails_gem.version.version}"
<         require rails_gem.full_gem_path + '/lib/initializer'
<       else
<         STDERR.puts %(Cannot find gem for Rails =#{rails_gem_version}.0:
<     Install the missing gem with 'gem install -v=#{rails_gem_version} rails', or
<     change environment.rb to define RAILS_GEM_VERSION with your desired version.
<   )
---
>       begin
>         gem "rails", rails_gem_version
>         gem_dir = Gem.path.select{|p| File.directory?("#{p}/gems/rails-#{rails_gem_version}")}.first
>         require "#{gem_dir}/gems/rails-#{rails_gem_version}/lib/initializer"
>       rescue Gem::LoadError => load_error
>         $stderr.puts %(Missing the Rails #{rails_gem_version} gem. Please `gem install -v=#{rails_gem_version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)

Hope this helps with those annoying warnings.

1 comment

Generate a Rails version specific project

October 09th, 2008 | Category: Development,Rails

If you have rails 2.x installed but need to generate a rails 1.x project, you can use the version tag as follows:

rails _1.2.6_ project

Just replace the 1.2.6 with whatever version you need, and make sure you use underscores before and after the version.

No comments

Ruby IRB Rails script/console tricks

September 10th, 2008 | Category: Development,Rails

I use script/console to play with any new features that I’m doing before they even make it into my app. I love using irb and script/console, but there were a few things that frustrated me.
1. Exit and reload the console when I make model changes
2. Command history was blown away on exit

So I decided to take some time to see if I could come up with solutions for the above. Luckily I found solutions for both of my problems.
Issue #1. After you make changes simply type “reload!” at the prompt to reload the app. This solved most of my problems, but still if I exit I lose all my previous commands.

Issue #2. Create the file ~/.irbrc to configure your irb settings. Include the following settings:

require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 200
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-history"

Voila, you now have a saved history just like a bash console. Use the “Up” and “Down” keys to browse your old history.

No comments

Next Page »