Hello all I''m having an issue with ruby on rails, and it not throwing an error where it should. I have a class ''clientpool'' that is: class Clientpool < ActiveRecord::Base set_table_name "clientpool" set_primary_key "id" belongs_to :clients, :foreign_key => "cliname" validates_presence_of :cliname validates_associated :client end However, when I try to make a new clientpool, that has an invalid cliname, that is not listed in clients, it will still create it and not error out. I see in the output log it does run the SQL to check, and the SQL querry returns 0 rows, so it should error out, but it doesn''t: SQL (0.000088) BEGIN Client Load (0.000115) SELECT * FROM clients WHERE (clients.id 444) LIMIT 1 SQL (0.000279) INSERT INTO clientpool (`cliname`, `prefix`, `did`) VALUES(444, 0, 0) SQL (0.000072) COMMIT Anyone got any ideas? Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Jakob Skjerning
2006-Oct-15 08:59 UTC
Re: issues with validates_associated not throwing error
On Oct 14, 2006, at 21:07, Mike Adam wrote:> I''m having an issue with ruby on rails, and it not throwing an error > where it should.[SNIP]> However, when I try to make a new clientpool, that has an invalid > cliname, that is not listed in clients, it will still create it and > not error out.I think you''ve misunderstood what the various validations do.> validates_presence_of :clinameAll this does is make sure the cliname attribute is present on the clientpool object, ie is not blank> validates_associated :clientThis ensures that the associated client object is valid, ie is passing its own validations. And another thing:> belongs_to :clients, :foreign_key => "cliname"You probably want belongs_to :client, not :clients, since the model can only belong to a single client. -- Jakob Skjerning - http://mentalized.net --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Jakob Skjerning wrote:> > I think you''ve misunderstood what the various validations do. > > > validates_presence_of :cliname > > All this does is make sure the cliname attribute is present on the > clientpool object, ie is not blank > > > validates_associated :client > > This ensures that the associated client object is valid, ie is > passing its own validations. > > And another thing: > > > belongs_to :clients, :foreign_key => "cliname" > > You probably want belongs_to :client, not :clients, since the model > can only belong to a single client. >I appreciate your changes, I did make them, it still does the same SQL, but still is allowing invalid :cliname to be created that don''t link to a :client How am I to alter how it validates a :client so that it will ensure only valid ones are there? class Clientpool < ActiveRecord::Base set_table_name "clientpool" set_primary_key "id" belongs_to :client, :foreign_key => "cliname" validates_associated :client, :on => :create validates_presence_of :cliname end --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---