I ran into a bit of a pickle earlier this week with some models and their relationships.
The Situation* A partner has an account.
* An account has an address.
* Accounts do not require an address; it's optional.
* Partners do require an address.
The ProblemSo how do I enforce the presence of an address for the account assigned to the partner?
My First AttemptMy first thought was to break the encapsulation and create a direct partner-to-address relationship. Then I came up with the clever hack of keeping the partner-address in sync with the account-address during assignment, so when you set the address for the partner, it would automatically set the account to the same address. But it just didn't sit right with me; it wasn't the typical Ruby on Rails elegance I had come to expect. So I reached out to my partner in crime (Jack Danger) and asked for his opinion.
A More Elegant IdeaJack suggested a more elegant simplification of just validating the presence of the address from the partner level like so:
validates_presence_of :addressNow that's the simplicity and elegance I've come to expect from a Ruby on Rails application.
account && account.address