Represent multiple Null/Generic objects in an ActiveRecord association?
Posted
by
slothbear
on Stack Overflow
See other posts from Stack Overflow
or by slothbear
Published on 2011-11-20T21:47:58Z
Indexed on
2011/11/21
1:50 UTC
Read the original article
Hit count: 362
I have a Casefile model that belongs_to a Doctor. In additional to all the "real" doctors, there are several generic Doctors: "self-treated", "not specified", and "removed" (it used to have a real doctor, but no longer does). I suspect there will be even more generic values in the future.
I started with special "doctors" in the database, generated from seed. The generic Doctors only need to respond to the "name" and "real_doctor?" methods.
This worked with one, was strained with two, and now feels completely broken. I want to change the behavior and can't figure out how to test it, a bad sign. Creating all the generic objects for testing is also trouble, including fake values to pass validation of the required Doctor attributes.
The Null Object pattern works well for one generic object. The "name" method could check casefile.doctor.nil? and return "self-treated", as demonstrated by Craig Ambrose.
What pattern should I use when there are multiple generic objects with very limited state?
© Stack Overflow or respective owner