class Category < ActiveRecord::Base belongs_to :sector has_and_belongs_to_many :suppliers validates :sector, :name, :presence => true As you see I have validates :sector, :presence => true. When I create a new Category, if I don''t select a sector I have an error page with the message: Couldn''t find Sector with ID But there is the validates, why I don''t have the message sector can''t be blank? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Feb 23, 2011 at 5:37 PM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class Category < ActiveRecord::Base > belongs_to :sector > has_and_belongs_to_many :suppliers > validates :sector, :name, :presence => true > > As you see I have validates :sector, :presence => true. > When I create a new Category, if I don''t select a sector I have an > error page with the message: > >I don''t know about the others but when associating other tables like this, I don''t do the validations this way. i create a method that i can call with validates. validates :sector_should_not_be_blank def sector_should_not_be_blank errors.add(:sector, "can''t be blank") if sector.nil? end> Couldn''t find Sector with ID> > But there is the validates, why I don''t have the message sector can''t be > blank? > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 23 February 2011 09:37, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class Category < ActiveRecord::Base > belongs_to :sector > has_and_belongs_to_many :suppliers > validates :sector, :name, :presence => true > > As you see I have validates :sector, :presence => true. > When I create a new Category, if I don''t select a sector I have an > error page with the message: > > Couldn''t find Sector with IDIs that error occurring before the save happens, or are you saying that it has saved it with no sector? Colin> > But there is the validates, why I don''t have the message sector can''t be blank? > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
On 23 February 2011 11:43, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 23 February 2011 09:37, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> class Category < ActiveRecord::Base >> belongs_to :sector >> has_and_belongs_to_many :suppliers >> validates :sector, :name, :presence => true >> >> As you see I have validates :sector, :presence => true. >> When I create a new Category, if I don''t select a sector I have an >> error page with the message: >> >> Couldn''t find Sector with ID> > Is that error occurring before the save happens, or are you saying > that it has saved it with no sector?I had an error in the controller sorry. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.