I’ve been going back and forth about this kind of thing for a while.
I reverse my opinion on it every project, and I’m wondering if anyone
has any advice/battle scars to share. As usual, please forgive my
long-windedness in advance.
Let’s say I have this:
Table: PLANS
ID int
CATEGORY_CODE_ID int
–Other junk removed for clarity
Table: CATEGORY_CODES
ID int
DESCRIPTION varchar(200)
“category codes”, to the user, are 6-digit numbers, sometimes with
leading zeroes, that have specific meanings.
My customers usually want a metric truckload full of reports. Often,
there are more reports than actual business logic. So much fun.
Reports get much, much easier when you don’t need to join over into a
lookup table constantly.
It would be nice to able able to simply say:
Plans.find_all_by_category_code ‘123456’
If I follow the ‘pure’ ActiveRecord intent for this, it would instead
look like:
Table: PLANS
ID int
CATEGORY_ID int
Table: CATEGORIES
ID int
CATEGORY_CODE char(6)
DESCRIPTION varchar(200)
…and I would then need to do:
Plans.find(:all, :conditions => “categories.category_code = ‘123456’”,
:include => :categories), which would be much slower.
…and then @some_plan.category.description whenever I needed to
display the description text.
In this design, ‘PLANS.CATEGORY_ID’ would be a non-semantic synthetic
value. Any find_by_sql queries would be much more tedious to write.
Am I going to run into trouble by having my ‘code table’ be an
association with a primary key that actually means something, rather
than being synthetic?
The code works either way, but I hate feeling like I’m going against
the grain with ActiveRecord.
Anyone have any comments or complaints?
Thanks,
–Wilson.