Rails Migrations: Populating/Updating Data

Recently I added the ability to set a publish date to blog posts. It would allow me to set a date in the future, and have the website not display the post until that date. The code I used to accomplish this can be found both here and here. That part was straight forward. The problem I ran into was setting the published_at date on all my previous posts. Just running the migration to add the published_at column set the value to nil on every post in the database. That wouldn't work, it would ruin the archives page, since everything that was once created_at is now published_at.

It only took a couple tries to come up with a solution. The main was adding the published_at column, and populating it right away with a date. Pretty or otherwise, here's the migration code:

class AddPublishedAtToPosts < ActiveRecord::Migration
  def self.up
    add_column :posts, :published_at, :datetime

    Post.all.each do |post|
      post.published_at = post.created_at
      post.save

      puts "'#{post.title}' published date updated to #{post.created_at}\n"
    end
  end

  def self.down
    remove_column :posts, :published_at
  end
end

A Simple problem, one of a few simple solutions, hopefully this saves someone a headache.

Continue Reading…

Oh Joy!

Happy New Years!

Continue Reading…

Version 4 Now on GitHub

Gifts all around! Here's the promised source code to the fourth iteration of MatHarvard.ca: github.com/matharvard/matharvardca_2008. Take a look at README.markdown for setup instructions; email me if you have any problems making it run.

Happy holidays!

A Weekly Affair

Other then the new design and the source code of the previous version being released on GitHub soon. I admit it's pretty quiet around here. So to redeem my self, I plan to learn one new thing about Ruby or Rails each and every week, then write about it here. It forces me to learn, and hopefully, even if it's one in a hundred, someone else will benefit from the article.

Previous Version Source

I've never actually "open sourced" anything before, shared it around with friends sure, but never put it online for the masses. To change that, I'm going to release the previous version of my site on GitHub. The only reason(s) that I haven't done it already are that I need to remove/protect certain files (deploy recipes, git repo locations, etc.) so no bad people find out my secrets! It will be out by the holidays, my gift to everyone. For now, enjoy the new website.