Archive for May, 2009

Rails: Expiring a cached page with namespaces and sweepers

Posted 31 May 2009 — by admin
Category Ruby on Rails

I’ve got some pages that are cached using their permalinks on the filesystem, such as http://example.com/about-us.html which will need to map to RAILS_ROOT/public/about-us.html … The issue I have is that I use a namespace for the admin area and the controllers in the namespace are responsible for expiring the cached pages, i.e., when the resources are updated by an admin.

Check out Rails Envy for a great tutorial for getting page caching set up: http://www.railsenvy.com/2007/2/28/rails-caching-tutorial

So what I want to do is expire the pages from inside my namespace. To accomplish this I need to use the pages route in my sweeper class.

class PageSweeper < ActionController::Caching::Sweeper
  #... after_save, after_destroy... we'll exprire the cache
  def expire_cache_for(record)
    #permalink rather than record id like /213.html
    expire_page(pages_path(record.permalink))
  end
end

Since I'm using a permalink to cache the page, I need to expire it with the permalink too.

Custom Date Formats for Your Rails Application

Posted 13 May 2009 — by admin
Category Ruby on Rails

If you use a consistent date format often in your Rails applciation, it is worth it to add the format to your application environment. You can do this by adding  it to the bottom of the config/environment.rb file.

Time::DATE_FORMATS[:my_custom_format] = "%A %B %d, %Y"

Now you can use it in your views like this

@post.created_at.to_s(:my_custom_format)

Which will output something like Monday May 5, 2009

Sending eMail with Rails on Mac OS X Development Environment

Posted 10 May 2009 — by admin
Category Programming

You’ll need a mail transport agent (MTA). I installed and used postfix using Mac Ports.

sudo port install postfix

You’ll need to start postfix, to send mail from your Rails application. You can set it as a startup item and it will start on boot. However, since I don’t send too much mail from my Rails app, just for testing normally, I start it manually.

sudo postfix start

That should do it!

Quick Syntax to Pipe an SQL Query Directly to a file

Posted 09 May 2009 — by admin
Category Programming

Here is a quick way to put the contents of a database table into a simple text file. This could be handy if for example, you just want to grab some emails and pop the results into a simple csv file. Your sql statement can be as creative as sql allows. All you are doing here is piping the query to mysql and then saving it to a file.

echo 'select concat(firstname, ', ', lastname, ', ', email) from email_subscribers' \
| mysql -uroot -p emails_database > emails.csv

Rails Migrations and Auto Incrementing Migration Prefix Number

Posted 09 May 2009 — by admin
Category Ruby on Rails

In your environment.rb file turn off timestamped migrations like so:

  config.active_record.timestamped_migrations = false

How to Install Ferret, the Full Text Search Engine with Your Rails Application

Posted 07 May 2009 — by admin
Category Ruby on Rails

Ferret is a full text search engine based on the popular Lucene Engine, which is originally written for Java. There is a great tutorial available here: http://www.railsenvy.com/2007/2/19/acts-as-ferret-tutorial

Assuming you have the plugin installed, acts_as_ferret in your vendor/plugins directory you’ll need to install the ferret engine on your box. Use gems like so

gem install ferret

If you’re running in dev mode, it should just work automatically, however in production, remember to start your ferret server in production mode with the following command!
./script/ferret_server start -e production

Starting the Rails Console in Production Mode

Posted 06 May 2009 — by admin
Category Ruby on Rails

To specify which mode you’d like the rails console to boot up in, just provide the string without any flags.

./script/console production
./script/console test
./script/console development

If you’re on windows, remember the backslash “\” rather than forward “/” and I believe you’ll also have to feed the ruby interpreter as well, like so…

ruby script\console production

Sample Rails Database Config for MySQL

Posted 01 May 2009 — by admin
Category Ruby on Rails

Sample Ruby on Rails database config file for connecting to mysql.

production:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: db_production
  pool: 5
  username: db_user
  password: db_password
  #socket: /tmp/mysql.sock #this may vary