Fun with active_record and sqlite3

Active Record, the ORM layer in the Rails cake (and camping, and Merb, etc) is strong magic.

Before stumbling on this trick, I would create new rails projects for every little thing I needed to figure out. Running
rails test_proj -d sqlite3
is just so easy!

Yesterday, I needed to figure out a way to have a search term provide alternates (“dog” might have “puppy”, “hound”, etc.) After thinking about it for a few minutes, I decided I needed to figure out how to play with ActiveRecord without a full rails project. Here it is: ar_playground

[source:ruby]
#!/usr/bin/env ruby

%w|rubygems active_record irb|.each {|lib| require lib}

ActiveRecord::Base.establish_connection(
:adapter => “sqlite3″,
:database => “test.db”
)

ActiveRecord::Schema.define do
create_table :fruits, :force => true do |t|
t.column :name, :string
end
end

class Fruit < ActiveRecord::Base
validates_presence_of :name
end

Fruit.create(:name => “apple”)

IRB.start if __FILE__ == $0
[/source]

This will create a sqlite3 database in the directory where it’s run, and drop you into an IRB shell with all the Classes available for playing.

If you have trouble with sqlite3, you can use mysql, or get sqlite3 running by checking out:
SQLite3 announcement on Jamis’ blog

If you’re curious what I came up with for my problem, here’s the pastie.

Peglist – my first real camping project

Update: I forgot to link to the repos! Here it is:
http://opus.winelibrary.com/peglist/trunk/.
There is a key for a popular photosharing site in there (rhymes with ticker), please don’t abuse it.

Camping, a ruby “micro-framework” is really a lot of fun once you get into it. It has sessions, ActiveRecord, MVC and a few other niceties that are great for “quick” things that don’t need to be full Rails projects.

So, in my spare time I’ve been playing with rewriting a php tool I wrote a while back for helping me memorize my Peg list. I made it for a few reasons: To play with Camping again, to work on my design skills, and make my old system much more usable.

Peg list by Meta | ateM.

    I wanted anyone to be able to play with the tool, I’ve added a few features that should allow anyone to jump right in, even if they have no interest in memory pegs:

  1. OpenID Logins
    These were pretty easy to get working in Camping. Even so, I’d like to make (or have) a plugin for Camping that would let me drop in OpenID auth anywhere.
  2. an “I’m too lazy for this. Do it for me.” button
    I figured that most people I showed this too wouldn’t really care about pegs. What better way then to pre-populate pegs for those people?
  3. Avatars from Flickr and Twitter
    Why bother writing an image uploader / cropper when I can just take a username and grab an existing avatar? I could add other services easily (like gravatar) and I might do that one day!
  4. Flickr API integration
    This is one of the few things my old tool did have. I’ve expanded it and made searching for an image easier and slicker.
  5. A Favicon!
    I always forget to make favicons. Thanks to Dan at Simplebits (from a recent set of slides he put up), I decided that this project HAD to have a favicon. It wasn’t hard, and ads a bit of polish to the site.