Hi all I''m a bit confused whether it''s better to check for the ID of a related object or the related object itself? validates_presence_of :user # is this better? validates_presence_of :user_id # or is this better? What would you suggest? Thanks, Josh -- 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 -~----------~----~----~----~------~----~------~--~---
validates_presence_of :user_id will test if there is a value for self.user_id regardless of whether there is a valid user with that id on the database. If you''re going to use :user_id then you should include validates_associated :user to check the user is valid validates_presence_of :user will check that the associated user is a valid record. On Apr 16, 2:09 pm, Joshua Muheim <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi all > > I''m a bit confused whether it''s better to check for the ID of a related > object or the related object itself? > > validates_presence_of :user # is this better? > validates_presence_of :user_id # or is this better? > > What would you suggest? Thanks, > Josh > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for your explanation. :-)> validates_presence_of :user will check that the associated user is a > valid record.so if the user id is nil, then there is no error? -- 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 -~----------~----~----~----~------~----~------~--~---
validates_presence_of :user_id only tests that there is a value for user_id with this method, user_id could be any integer, not necessarily a valid user. validates_presence_of :user will also check that the user is a valid record on the database. That clear it up?> so if the user id is nil, then there is no error? > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Gavin wrote:> validates_presence_of :user_id only tests that there is a value for > user_id > > > with this method, user_id could be any integer, not necessarily a > valid user. > > validates_presence_of :user will also check that the user is a valid > record on the database. > > That clear it up?so validates_presence_of :user does the same like the combination of validates_presence_of :user_id and validates_associated :user... right? thanks for the explanation. :-) -- 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 -~----------~----~----~----~------~----~------~--~---