In the docs it says I should validate the presence of the foreign key and not the object itself. I did this and I still get the error when both objects are new records. A very simple example: class UserGroup < ActiveRecord::Base has_many :users, :dependent => :destroy end class User < ActiveRecord::Base belongs_to :user_group validates_presence_of :user_group_id end user_group = UserGroup.new user_group.users.build user_group.save! Tells me that users is invalid and it says that user group cant be blank. In the docs it says if I do this I should not get this error. Is this a new edition to ActiveRecord or something? I am using edge rails. Thanks! -- 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 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?hl=en -~----------~----~----~----~------~----~------~--~---
The problem is that user_group.id is nil, so when build is called, the child User object that''s created has a nil foreign key. The save on the next line fails for this reason. If you replace UserGroup.new with UserGroup.create!, I''ll bet it would work. That''s my take, at least, and it matches the behavior I''ve seen: You have to save the new parent before adding children. This does not match my understanding of the Agile book, but I''ve written some tests, and that''s what seems to be necessary. ///ark --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Ben Johnson wrote:> In the docs it says I should validate the presence of the foreign key > and not the object itself. I did this and I still get the error when > both objects are new records. A very simple example: > > > class UserGroup < ActiveRecord::Base > has_many :users, :dependent => :destroy > end > > class User < ActiveRecord::Base > belongs_to :user_group > validates_presence_of :user_group_id > end > > > user_group = UserGroup.new > user_group.users.build > user_group.save! > > > Tells me that users is invalid and it says that user group cant be > blank. In the docs it says if I do this I should not get this error. Is > this a new edition to ActiveRecord or something? I am using edge rails. > Thanks!To my mind the API doc''s advice to validate presence of foreign keys rather than associations is wrong, as I have written about before: http://groups.google.com/group/rubyonrails-talk/msg/0c827209dd10e767 Validating the presence of associations is the Rails equivalent of DB fk constraints. Validating the presence of foreign_key attributes just checks that they''re not null. They could still point to non-existent records. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Thu, 13 Dec 2007 13:52:27 +1100, Mark Reginald James wrote:> http://groups.google.com/group/rubyonrails-talk/msg/0c827209dd10e767 > > Validating the presence of associations is the Rails equivalent of DB fk > constraints. Validating the presence of foreign_key attributes just > checks that they''re not null. They could still point to non-existent > records.Ohh, thanks for the information. -Thufir --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---