has_one() in AR kind of weak?

Hi,

suppose I'm doing a rails system for the DMV.

The rules say:

A person may have 0 or 1 Drivers License.

So, I'd have these 2 tables:

class CreatePeople < ActiveRecord::Migration   def self.up     create_table :people do |t|       t.column :name, :string     end   end

  def self.down     drop_table :people   end end

class CreateDriverslicenses < ActiveRecord::Migration   def self.up     create_table :driverslicenses do |t|       t.column :person_id, :integer     end   end

  def self.down     drop_table :driverslicenses   end end

And these models:

class Person < ActiveRecord::Base   has_one :driverslicense end

class DriversLicense < ActiveRecord::Base   belongs_to :person end

What is the best way to enforce this rule:

A person may have 0 or 1 Drivers License. ??

I ran a simple test with console:

pete = Person.new

=> #<Person:0x2364d9c @attributes={"name"=>nil}, @new_record=true>

pete.name = 'Pete'

=> "Pete"

dl1 = Driverslicense.new

=> #<Driverslicense:0x235ac0c @attributes={"person_id"=>nil}, @new_record=true>

dl1.person_id = pete

=> #<Person:0x2364d9c @errors=#<ActiveRecord::Errors:0x23511ac @errors={}, @base=#<Person:0x2364d9c ...>>, @attributes={"name"=>"Pete", "id"=>1}, @new_record=false>

dl1.person.name

=> "Pete"

So, now Pete has a drivers license.

Let's try to give him another drivers license:

dl2 = Driverslicense.new

=> #<Driverslicense:0x233de2c @attributes={"person_id"=>nil}, @new_record=true>

dl2.person_id = pete

=> #<Person:0x2364d9c @errors=#<ActiveRecord::Errors:0x23511ac @errors={}, @base=#<Person:0x2364d9c ...>>, @attributes={"name"=>"Pete", "id"=>1}, @new_record=false>

dl2.person.name

=> "Pete"

It appears that dmv has assigned 2 drivers licenses to Pete.

Is it up to me to 'strengthen' the has_one() method in AR?

Here is info about my rails:

$ script/about About your application's environment Ruby version 1.8.4 (powerpc-darwin7.9.0) RubyGems version 0.9.0 Rails version 1.1.6 Active Record version 1.14.4 Action Pack version 1.12.5 Action Web Service version 1.1.6 Action Mailer version 1.2.5 Active Support version 1.3.1

-Pete http://GoodJobFastCar.com goodjobfastcar@gmail.com

You could add a uniqueness validation on the Driverslicense model

class Driverslicense < ActiveRecord::Base

validates_uniqueness_of :person_id

end

-Jonathan.