AJ Schuster
2005-Jun-16 15:01 UTC
validates_associated doesn''t actually validate anything
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
Scott Barron
2005-Jun-16 17:10 UTC
Re: validates_associated doesn''t actually validate anything
On Jun 16, 2005, at 11:01 AM, AJ Schuster wrote:> 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. >validates_associated doesn''t quite work that way. You need to actually associate the objects first. For example, this would fail: # Modify Site to show an invalidity class Site < ActiveRecord::Base has_many :contracts validates_presence_of :name end c = Contract.new s = Site.new c.site = s c.save => false because the site is invalid (we gave it no name) If you need to use the id numbers you''ll probably need to write your own custom validation code, otherwise you could do something like: c = Contract.new c.site = Site.find(params[:site_id]) c.customer = Customer.find(params[:site_id]) c.save and handle any errors occuring there (e.g. find throwing RecordNotFound, or using any of the other finders and their errors). -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
AJ Schuster
2005-Jun-16 18:24 UTC
Re: validates_associated doesn''t actually validate anything
On 6/16/05, Scott Barron <scott-HDQKq3lYuGDk1uMJSBkQmQ@public.gmane.org> wrote:> > If you need to use the id numbers you''ll probably need to write your > own custom validation code, otherwise you could do something like: > > c = Contract.new > c.site = Site.find(params[:site_id]) > c.customer = Customer.find(params[:site_id]) > c.save > > and handle any errors occuring there (e.g. find throwing > RecordNotFound, or using any of the other finders and their errors).Scott- Thanks for the suggestion. That worked perfectly. Upon rereading the documentation for validates_associated, I suppose this behavior makes sense. AJS