If I have belongs_to :user Do I need to have validates_presence_of :user_id ? Does Rails validate the presence of :user_id automatically if I have belongs_to :user? In other words, does Rails prevent against creating orphan records that belong to non-existent users? Thanks. -- Posted via http://www.ruby-forum.com/.
2009/7/4 Learn by Doing <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > If I have > > belongs_to :user > > Do I need to have > > validates_presence_of :user_id > > ? > > Does Rails validate the presence of :user_id automatically if I have > belongs_to :user? > > In other words, does Rails prevent against creating orphan records that > belong to non-existent users? >I believe rails will not stop you creating a record with an empty user_id. To be pedantic this is ''does not belong to any user'' rather than ''belongs to a non-existent user'' (for a non-existent user the user_id would contain a non-nil value for which there is no user). In some applications this is a valid requirement. If you wish to prevent this then include validates_presence_of as you have indicated. Colin
Colin Law wrote: [...]> I believe rails will not stop you creating a record with an empty > user_id. To be pedantic this is ''does not belong to any user'' rather > than ''belongs to a non-existent user'' (for a non-existent user the > user_id would contain a non-nil value for which there is no user).Correct.> In > some applications this is a valid requirement. If you wish to prevent > this then include validates_presence_of as you have indicated.Also make the field not null in the DB and add a foreign key constraint (the foreign_key_migrations and foreign_key_associations plugins make this easy). Remember, Rails'' validations are not a replacement for the DB''s integrity checks. Don''t rely solely on Rails for this.> > ColinBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Thanks Marnen and Colin for your excellent suggestions. -- Posted via http://www.ruby-forum.com/.
Hi Colin, After reading the API, I think that validates_presence_of :user_id will not even check to make sure that the user exists in the user table. It only makes sure that the field value is not blank: http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_presence_of. So this validation does not do the work of a proper foreign key constraint. Am I reading the API incorrectly? Thanks. On Jul 4, 2:14 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 2009/7/4 Learn by Doing <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: > > > > > > > If I have > > > belongs_to :user > > > Do I need to have > > > validates_presence_of :user_id > > > ? > > > Does Rails validate the presence of :user_id automatically if I have > > belongs_to :user? > > > In other words, does Rails prevent against creating orphan records that > > belong to non-existent users? > > I believe rails will not stop you creating a record with an empty > user_id. To be pedantic this is ''does not belong to any user'' rather > than ''belongs to a non-existent user'' (for a non-existent user the > user_id would contain a non-nil value for which there is no user). In > some applications this is a valid requirement. If you wish to prevent > this then include validates_presence_of as you have indicated. > > Colin
Quoting Learn By Doing <easebus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Hi Colin, > > After reading the API, I think that > > validates_presence_of :user_id > > will not even check to make sure that the user exists in the user > table. It only makes sure that the field value is not blank: > http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_presence_of. > > So this validation does not do the work of a proper foreign key > constraint. Am I reading the API incorrectly? >Yes, there is another validation to verify that the associated object exists and is valid, validates_associated. HTH, Jeffrey
Jeffrey: Thank you. I tried that but it still didn''t work. So I have this class Stat < ActiveRecord::Base belongs_to :user validates_presence_of :user_id validates_associated :user The user table is empty. I then create a stat with user_id = 99, which does not exist in the user table. No error is produced: Loading development environment (Rails 2.3.2)>> stat = Stat.new(:user_id => 99)=> #<Stat id: nil, user_id: 99, ...>>> stat.save!=> true>>A new record with user_id = 99 was created in the database although the associated user does not exist. Perhaps this validation only validates the associated user when that user exists. See API: http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_associated Did I read it right? Thanks. -- Posted via http://www.ruby-forum.com/.
There is nothing in rails that can validate an associated record exists for a belongs_to relationship. But you can use Josh Susser''s fantastic validates_existence_of plugin that does check whether the associated record exists or not Check out his post here here http://blog.hasmanythrough.com/2007/7/14/validate-your-existence On Jul 5, 8:44 am, Learn by Doing <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Jeffrey: > > Thank you. I tried that but it still didn''t work. So I have this > > class Stat < ActiveRecord::Base > > belongs_to :user > validates_presence_of :user_id > validates_associated :user > > The user table is empty. I then create a stat with user_id = 99, which > does not exist in the user table. No error is produced: > > Loading development environment (Rails 2.3.2)>> stat = Stat.new(:user_id => 99) > > => #<Stat id: nil, user_id: 99, ...> > > >> stat.save! > => true > > A new record with user_id = 99 was created in the database although the > associated user does not exist. Perhaps this validation only validates > the associated user when that user exists. See API:http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validat... > > Did I read it right? > > Thanks. > -- > Posted viahttp://www.ruby-forum.com/.
Thanks nas. I am surprised that this has not made it to Rails 2.3.2. On Jul 5, 10:11 pm, nas <nasi...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> There is nothing in rails that can validate an associated record > exists for a belongs_to relationship. > > But you can use Josh Susser''s fantastic validates_existence_of plugin > that does check whether the associated record exists or not > > Check out his post here herehttp://blog.hasmanythrough.com/2007/7/14/validate-your-existence > > On Jul 5, 8:44 am, Learn by Doing <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > > > Jeffrey: > > > Thank you. I tried that but it still didn''t work. So I have this > > > class Stat < ActiveRecord::Base > > > belongs_to :user > > validates_presence_of :user_id > > validates_associated :user > > > The user table is empty. I then create a stat with user_id = 99, which > > does not exist in the user table. No error is produced: > > > Loading development environment (Rails 2.3.2)>> stat = Stat.new(:user_id => 99) > > > => #<Stat id: nil, user_id: 99, ...> > > > >> stat.save! > > => true > > > A new record with user_id = 99 was created in the database although the > > associated user does not exist. Perhaps this validation only validates > > the associated user when that user exists. See API:http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validat... > > > Did I read it right? > > > Thanks. > > -- > > Posted viahttp://www.ruby-forum.com/.- Hide quoted text - > > - Show quoted text -
nas wrote:> There is nothing in rails that can validate an associated record > exists for a belongs_to relationship. > > But you can use Josh Susser''s fantastic validates_existence_of plugin > that does check whether the associated record exists or not > > Check out his post here here > http://blog.hasmanythrough.com/2007/7/14/validate-your-existence > > On Jul 5, 8:44�am, Learn by Doing <rails-mailing-l...@andreas-s.net>I''ve found that: validates_presence_of :user_id validates_presence_of :user works just fine. From what I have been able to tell, validates_associated is meant for when you are constructing two or more associated models at the same time, and you don''t want to save them unless both validate. So your user validates would contain: validates_associated :address which would run all the validations for the address model. However, you can''t at the same time have a validates_associated :user line in the address model, because that will cause an infinite recursion loop of validations. -- Posted via http://www.ruby-forum.com/.