GitHub Now Open to the Public, the “easiest (and prettiest)” version control system, is now open to the public. It’s free for Open Source programs (albeit limited to 100MB of space).

If you’re not sure what git (or Revision Control) is, here are some resources:
Wikipedia on Revision Control

(excerpt) “Revision control (also known as version control (system) (VCS), source control or (source) code management (SCM)) is the management of multiple revisions of the same unit of information. It is most commonly used in engineering and software development to manage ongoing development of digital documents like application source code, art resources such as blueprints or electronic models, and other critical information that may be worked on by a team of people. Changes to these documents are usually identified by incrementing an associated number or letter code, termed the ‘revision number’, ‘revision level’, or simply “revision” and associated historically with the person making the change. A simple form of revision control, for example, has the initial issue of a drawing assigned the revision number ‘1’. When the first change is made, the revision number is incremented to ‘2’ and so on.”

(A cocky) Linus Torvalds talking on git:

Flame On!

Subversion vs git vs other?
Flame Control System

Simple Way to Populate a Database in Rails

This is how I populate my database when I have a lot of data but can’t be bothered to write more than a quick throw-away hack. This doesn’t use fixtures, nor migrations (nothing wrong with them, I wuv migrations). Just a ruby file and the Rails console (this is optional actually).

I create a new rb file in lib/ (you can put your files in a sub-directory or anywhere ‘load’ can find them), then write the data I want inserted into the database in a new function in that file. I do so exactly as I would insert data in Rails. Model.create,, etc…

I then get into the Rails console (ruby script/console) and do load ‘file.rb’ and simply call the function. The database used will vary according to the environment you’re in (test, development, etc). Using ‘load’ every time you call your function(s) is preferred. Load will keep reloading the file (as opposed to ‘require’ which only reads a file once), staying up to date with edits you’re making to the file. For example, in the console:

>> load ‘funk.rb'; add_default_settings

(funk.rb is in /lib/ and add_default_settings is a function in that file)

As of 2.x, Rails now has rake db:migrate:reset and db:migrate:redo, which sends you down one migration and then back up to the current migration (or you can decide how many hops to take back using STEP=n). This is great, especially for tasks like populating a database.

So why don’t I use migrations? I do, but sometimes, especially in a proof-of-concept or throw-away app, I find it faster to skip the proper methods and write up a quick function. If you think that’s bad, you should see my lingering Python addiction. Sometimes I catch myself metaprogramming Ruby code in Python.

Introduction to Validations & Validation Error Handling in Rails

Purple Nitraguard Security Keypad

Validations in Ruby on Rails are essentially nothing more than methods that ensure that the data in a model is valid before saving it to the database. Traditionally, we validate data coming in using conditional expressions (for example, if email != NULL or if passwd==passwd_confirmation). This task is essential, but boring and tedious, but Rails’ validations make this mundane part of programming as simple and as easy as possible. The validations provided in Rails (defined in every model) are thorough, likely covering all your needs right out of the box. There are even validations provided for checking whether a user agreed to a Terms of Service or End-User License Agreement (EULA), and for doing automatic field confirmation, useful when you ask the user for an email or password twice to ensure no misspelling.
Continue reading Introduction to Validations & Validation Error Handling in Rails

Expanding List of Ruby on Rails Sites, Editors, Tutorials, Cheat Sheets and More

Most of these Ruby on Rails related links are right out of my bookmarks. I checked for 404s and added more recent entries, but this list is far from complete. New blogs, sites, tutorials and tools are released on a daily basis. Just leave a comment or contact me if you want your link(s) or anything related added, or have spotted any broken links.

(Updated 02/22/2008. Keep the suggestions coming!)

Continue reading Expanding List of Ruby on Rails Sites, Editors, Tutorials, Cheat Sheets and More

Understanding Basic Database Relationships in Rails

Nixon ERD

This short tutorial will be beneficial for you if database relationships and keywords like belongs_to and has_many confuse you, or if you’re trying to find out how relationships are implemented in Rails. As we create a small demonstration project, you’ll see that one beauty of Rails is how it does most of the work gluing everything together, after you’ve supplied it with information about your database’s structure.

But first — why bother learning about relationships? Very simply, they eliminate a major problem called an update anomaly, and they will probably save you disk space. Having info repeated in multiple entries can be problematic. How would you update a mass misspelling? Would you even notice a misspelled entry? Database normalization and multiple intertwined tables (via relationships) can curb this problem. Fortunately, ActiveRecord makes this easy.

For example, if you store the name and location of all your users in the same database table, you might be wasting disk space by having the same information repeated in multiple entries. You would be wasting a lot of space if your clam-cake-vendor-review site has hundreds of users living in “the State of Rhode Island and Providence Plantations.” This can be eliminated by having the locations tied to unique IDs in their own table, and associated to a user by their ID. This also makes renaming a location easy. Changing “the State of Rhode Island and Providence Plantations” to “Ocean State” is only done in one location, once.

Rather than going over all possible types of relationships here, I will be covering the very basics; Enough to help you grasp the main idea and see how it is implemented in Rails. Let’s begin by designing a simple project.
Continue reading Understanding Basic Database Relationships in Rails

Using Vim as a Complete Ruby on Rails IDE

vi traced with an optical mouse

NOTE: If you are experiencing segmentation faults with vim and rails.vim, see this post.

When coding in Ruby on Rails, you’ll usually be switching between files and running scripts a lot. It can be time-consuming and frustrating coding Rails using a traditional text editor designed for working on big files individually. Vim lets you hop around within a file with enough speed to activate the cosmic treadmill – but without a plethora of hacks and custom key mappings, it’s weak as a Rails IDE. Fortunately, for those of us who are reluctant to kick the vim habit, Tim Pope comes to the rescue with rails.vim; A plugin that makes working with Rails in vim painless and efficient. In this guide, I will explain how to install and use rails.vim, along with a few other plugins you’ll find useful when writing Rails applications.
Continue reading Using Vim as a Complete Ruby on Rails IDE

Music to Code By – What Do You Listen to While Programming?

Yellow Vinyl Record Spinning

Some swear by it, for others it’s a bane: Music while coding /programming. Throughout grade school, we’ve been cautioned not to keep the TV and music on when doing homework, but is it really counter-productive? According to this study (link no longer works), not so, at least for people who are under-stimulated  which probably accounts for most us these days.

Let’s face it: We’re ridiculously overstimulated. How many of us don’t constantly have at least five Firefox instances open, each nesting a dozen or two tabs, F5ing Slashdot and Digg, keeping track of new emails and countless IRC channels, and crawling from one Wikipedia page to another endlessly?
Continue reading Music to Code By – What Do You Listen to While Programming?