Hello champs, I have a validation in model ABC and that is:- validates_presence_of :employee_id Now the record should not be saved without the presence of employee_id right. But now i wrote a test case like this:- def test_should_not_save_abcModel_without_employee_id abc = Abc.new(:name => "ABC Group") assert abc.save, "Saving the record without employee_id" end But still this record gets saved in database. Why so .. ? What i am doing wrong here.. My database migrations for employee_id column as well:- t.integer :employee_id, :null => false -- 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-/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.
it is impossible!! there is something wrong with model or database 2010/4/9 Hemant Bhargava <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>> Hello champs, > > I have a validation in model ABC and that is:- > validates_presence_of :employee_id > Now the record should not be saved without the presence of employee_id > right. But now i wrote a test case like this:- > > def test_should_not_save_abcModel_without_employee_id > abc = Abc.new(:name => "ABC Group") > assert abc.save, "Saving the record without employee_id" > end > > But still this record gets saved in database. Why so .. ? What i am > doing wrong here.. My database migrations for employee_id column as > well:- > > t.integer :employee_id, :null => false > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Anthony wrote:> it is impossible!! >Hey i know that is impossible.. That''s why i am asking you people.. i have posted my migrations and validations here as well.. Can you please look at them and tell me .. -- 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-/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.
In which file is this test written and how are you running it? Also, these kinds of tests are not very useful because they try to test the framework, ActiveRecord in this case. Tests should target the intended behavior of your application. -- 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.
You should remove :null => false. With it, you have a 0 in any new record, so validates_presence_of :employee_id will return true. -- 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.
Are you somehow inserting a blank in your model with a before_save or similar callback? What is the value of the column in the created record? Are you the only one with access to the DB tables? Could somebody have changed the column to allow now null values? On Apr 9, 7:45 am, Hemant Bhargava <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello champs, > > I have a validation in model ABC and that is:- > validates_presence_of :employee_id > Now the record should not be saved without the presence of employee_id > right. But now i wrote a test case like this:- > > def test_should_not_save_abcModel_without_employee_id > abc = Abc.new(:name => "ABC Group") > assert abc.save, "Saving the record without employee_id" > end > > But still this record gets saved in database. Why so .. ? What i am > doing wrong here.. My database migrations for employee_id column as > well:- > > t.integer :employee_id, :null => false > -- > 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-/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.