[rspec-users] RSpec makes me want to write better code
Ashley Moran
ashley.moran at patchspace.co.uk
Fri Sep 26 11:28:16 EDT 2008
On 26 Sep 2008, at 12:49, Matt Wynne wrote:
> Would you mind elaborating on why you don't like these? I'm pretty
> new to rails (but not programming generally) and rather naive about
> such things!
It's quite hard to explain briefly, but basically it makes the
predicate (interpretation of the table) extremely difficult.
A normal table like
cars
---------
reg_plate
owner_id
purchase_date
Could be interpreted as "The car with reg plate <reg_plate> was bought
by the person with ID <owner_id> on <purchase_date>" (ok, they might
have sold it since, but...)
However, from the Rails wiki[1]:
addresses
---------
street
city
country
addressable_id
addressable_type # <- is a string
How do you interpret this? The relationship is fundamentally
different depending on the value of addressable_type, and is much
harder to enforce as an integrity constraint*.
There's another angle you can take, based on data types - you can
define a type PERSON_ID for the attribute 'owner_id' (and re-use it in
the 'departments' table as 'manager_id'), but the type of
'addressable_id' depends on the value of 'addressable_type'. How can
a value not know its own type?
Easier solution:
people
---------
name
delivery_address_id
billing_address_id
"The person called <name> wants items delivered to the address with ID
<delivery_address_id> and invoices delivered to the address with ID
<billing_address_id>".
> Also why is the article so down on STI? What are the drawbacks? What
> do people use instead?
>
> [1]http://www.matthewpaulmoore.com/articles/1276-ruby-on-rails-code-quality-checklist
I'll reply to Mark...
TBC
Ashley
[1] http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations
* pah, who needs their data integrity protected anyway?
--
http://www.patchspace.co.uk/
http://aviewfromafar.net/
More information about the rspec-users
mailing list