validates_associated doesn''t actually validate anything

AJ Schuster
Thu, 16 Jun 2005 15:01:29 -0000

I am working with three models: Site, Customer, and Contract. Relevant
portions of classes follow:

class Site < ActiveRecord::Base
       has_many :contracts
end

class Customer < ActiveRecord::Base
       has_many :contracts
end

class Contract < ActiveRecord::Base
       belongs_to :customer
       belongs_to :site

       validates_associated :customer
       validates_associated :site
end

When I try to create a contract with invalid customer_id and site_id
fields, however, no errors are raised. I wind up with rows in my
contract table that reference nonexistent customer and site rows.

development.log shows that SOMETHING is happening... [unrelated
parameters deleted]

Processing ContractsController#create (for 127.0.0.1 at Thu Jun 16
09:56:12 Eastern Daylight Time 2005)
 Parameters: {"submit"=>"Create",
"action"=>"create",
"controller"=>"contracts",
"contract"=>{"site_id"=>"-120",
"customer_id"=>"-730",}}
[4;33mContract Columns (0.010000) [1;37mSHOW FIELDS FROM contracts
[4;35mContract Columns (0.010000) [0;37mSHOW FIELDS FROM contracts
[4;35mSite Load (0.000000) [0;37mSELECT * FROM sites WHERE sites.id -120 LIMIT 1
[4;33mCustomer Load (0.000000) [1;37mSELECT * FROM customers WHERE
customers.id = -730 LIMIT 1
[4;35mSQL (0.000000) [0;37mINSERT INTO contracts
(`site_id`,`customer_id`,) VALUES(-120, -730)
[4;33mSQL (0.010000) [1;37mCOMMIT
Redirected to http://localhost:3000/contracts/list
Completed in 0.29000 (3 reqs/sec) | DB: 0.03000 (10%)

Am I mis-using belongs_to, has_many, or validates_associated? Can
anyone offer some light on this...weird behavior?

AJS