Ben Johnson
2009-Jan-09 00:13 UTC
What is wrong with validates_presence_of :my_association ?
Category.has_many :products Let''s say a product must belong to a category. I know it is recommended to do the following: class Product validates_presence_of :category_id end But, as we all know, this is a pain for new records, meaning what if the associated category is a new record? I''ve been meaning to ask this, but what''s wrong with doing the following: class Product validates_presence_of :category end I never understood why that was so wrong. So I wanted to ask and see if I am missing something. What do you think? Thanks for your help! -- 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2009-Jan-09 00:59 UTC
Re: What is wrong with validates_presence_of :my_association ?
That will check the value for a field called category, not an association. ----- Ryan Bigg Freelancer http://frozenplague.net On 09/01/2009, at 10:43 AM, Ben Johnson wrote:> > Category.has_many :products > > Let''s say a product must belong to a category. I know it is > recommended > to do the following: > > class Product > validates_presence_of :category_id > end > > But, as we all know, this is a pain for new records, meaning what if > the > associated category is a new record? > > I''ve been meaning to ask this, but what''s wrong with doing the > following: > > class Product > validates_presence_of :category > end > > I never understood why that was so wrong. So I wanted to ask and see > if > I am missing something. What do you think? Thanks for your help! > -- > 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 -~----------~----~----~----~------~----~------~--~---
Xie Hanjian
2009-Jan-09 01:52 UTC
Re: What is wrong with validates_presence_of :my_association ?
* Ben Johnson <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> [2009-01-09 01:13:53 +0100]:> > Category.has_many :products > > Let''s say a product must belong to a category. I know it is recommended > to do the following: > > class Product > validates_presence_of :category_id > end > > But, as we all know, this is a pain for new records, meaning what if the > associated category is a new record? > > I''ve been meaning to ask this, but what''s wrong with doing the > following: > > class Product > validates_presence_of :category > endIn this case you may need validates_associated or write your own validation by def validate.> > I never understood why that was so wrong. So I wanted to ask and see if > I am missing something. What do you think? Thanks for your help! > -- > 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@googlegroups.com > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > -~----------~----~----~----~------~----~------~--~----- jan=callcc{|jan|jan};jan.call(jan)
Mark Reginald James
2009-Jan-09 02:02 UTC
Re: What is wrong with validates_presence_of :my_association ?
Ben Johnson wrote:> Category.has_many :products > > Let''s say a product must belong to a category. I know it is recommended > to do the following: > > class Product > validates_presence_of :category_id > end > > But, as we all know, this is a pain for new records, meaning what if the > associated category is a new record? > > I''ve been meaning to ask this, but what''s wrong with doing the > following: > > class Product > validates_presence_of :category > end > > I never understood why that was so wrong. So I wanted to ask and see if > I am missing something. What do you think? Thanks for your help!It isn''t wrong, it''s the right way to do it. You just have to assign a category object when building a new Product. -- Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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 -~----------~----~----~----~------~----~------~--~---