Archive for July, 2010

rails, lookup tables and direct id checks…and rainbows

Wednesday, July 28th, 2010

a questionable practice (but a practice non the less) is to build a domain model that encompasses every data you can think about and has remotely to do with your core data. if it really is practical to put everything and all things into tables and fetch them through ActiveRecord is a whole other talk (crazy talk i say) for an other time (crazy time if you ask me).

anyway

so we get persons as entities which could have students or employees associated which belong to organizations that have addresses which contain a country which has a country code and all have multiple status lookup models for all kind of things. And suddenly you get something like this in your controller or models only to speed things up so you don’t have to fetch the associated models every time you need to compare something (and don’t have to think about some smart comparisons based on attributes to identify them for real):

if person.gender_id == 1 # do something with the boys else # do something with the gals end

or even better:

item.status_id = 4 if item.status_id == 99

sounds intuitive doesn’t it? :/

all of a sudden you have cryptic domain logic and 3 years from now somebody will ask why there is a status_id 99 and what this all means and if there is a meaning of life and why i still dont have seen inception and nobody knows anymore cause the guy who wrote this is long gone (died cause his eyes started bleeding from all this code…)

or remember the one day an admin accidentally deleted the status with id 4 while working on the db and just added it again but now it has id 5 and 1000 users suddenly where not “inactive” anymore (nor were they really “active” but who checks if a user is active if you can also check if his status is not an inactive id of 4)

or the other time a developer added a new status to the fixture file (yes you heard right, fixture files – this is the future baby. yml files – so we are cool), just not to the end, so every user that was “looking for woman” was now “looking for unicorns” (and believe me, the two are not interchangeable all the time…just sometimes)

…ah, fun times

so this all depends on the id in lookup models to never change…ever, cause if it changes you have to hunt through your code

and thats exactly what i did for the last couple of hours. not because the lookup ids changed but to prevent this in future releases to happen

what did i do? something really revolutionary: define constants

def SomeStatus < ActiveRecord::Base StatusA = 1 StatusB = 2 StatusC = 3 end

so from now on you could easily check by the constants which are also nicely namespaced by the model they belong to

item.some_status_id == SomeStatus::StatusA

the only problem with this is that i still had to change all the code as if all ids had changed at once :( – and i did it only for one model right now, still ~13 to go

motto of the day: constants bitches – use em

next time we talk about why some things are just not meant to be keeped in databases…like rainbows…or babies