Tuesday, January 27, 2009

Rails ActiveRecord validation

For something so simple, you'd think there'd be a decent amount of documentation out there for it, but you'd be wrong.

I was trying to write a validation on a model to ensure a certain condition was met, in this case that one object attribute was never larger than another for the same object before it was saved. Turns out it was as simple as this (sorry for the formatting, not sure how to post code snippets yet):
_def validate
__errors.add_to_base('x cannot be greater than y') if self.x > self.y
Apparently, Rails looks for a validate method and will call it during the validation process. No need for callbacks here (i.e. before_update, after_validation, etc.). The corresponding test is fairly easy to deduce:
it "should have not have more x than y" do
_@obj.y = 0
_@obj.x = 1
_@obj.should_not be_valid

Friday, January 9, 2009

From ESPN Page 2's the Sports Guy, Bill Simmons

The Todd Marinovich Award for "Worst performance by a rookie QB"
Put it this way, Matty Ice: When the Cards are jumping the snap for four quarters, then one of them goes on a radio show saying they jumped every snap count because you called every play on "one," then you probably should enroll in a "How to vary your snap counts" class at Steve DeBerg College this summer. Although I love the potential of a Boston College product not being able to count to two.
Yea, I realize this isn't a very good first post, but deal with it.