Colin Law wrote:
> 2009/4/30 Colin Law <[email protected]>
>
>> Use
>> has_one :wife
>
>
> On second thoughts I am not sure about this, writing
>
> class Wife < ActiveRecord::Base
> belongs_to :husband
> end
>
> may well get you into serious trouble.
This is actually one of the more interesting examples. In the specific
case of husband and wife the design pattern that most people use is
something like the following:
Story:
Assuming the relationship is based on one spouse at a time a
many-to-many relationship is still used to track the history of
marriages between people. The one-spouse-at-a-time rule would then be
implemented in validation code.
Person < ActiveRecord::Base
has_many :marriages
has_many :spouses, :through => :marriage
validate :one_spouse_at_a_time
def current_spouse
# find and return the person's current spouse
end
protected
def one_spouse_at_a_time
# do the validation here
end
end
Something along that line anyway.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---