David Kennedy
2007-May-06 14:11 UTC
validates_uniqueness_of (with :scope) doesn''t seem to work?
I have a master record called ''project'' and a child record called ''agycode''. Obviously, agycode has a project_id FK. I wish to make the "descr" field unique ONLY within the ''project_id'' ''scope''. Here are the key pieces of information Agycode fields (id, project_id, descr) Here''s the declaration in the Agycode.rb validates_uniqueness_of *:descr*, *:scope* => "project_id" Here''s the test that SHOULDN''T pass but it does *def* test_should_fail_duplicate_descr record1 = *@project*.agycodes.*new*(*:descr* => "description") record2 = record1 assert record1.save, "Save Record" assert_valid(record1) assert record2.save, "Save Record" assert_valid(record2) *end* By the way, do you know how one would write the opposite of "assert_valid"? Thank you, David --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
eden li
2007-May-06 15:17 UTC
Re: validates_uniqueness_of (with :scope) doesn''t seem to work?
You''re saving the same record twice. The problem line: record2 = record1 This does *not* do a "deep copy" of record1''s attributes and create a new object. Instead it just creates a new variable with a reference to the same object that record1 references. Try: record2 = record1.clone Note that "clone" doesn''t copy associations. On May 6, 10:11 pm, "David Kennedy" <dauntlessda...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a master record called ''project'' and a child record called > ''agycode''. Obviously, agycode has a project_id FK. I wish to make the > "descr" field unique ONLY within the ''project_id'' ''scope''. Here are the key > pieces of information > Agycode fields (id, project_id, descr) > > Here''s the declaration in the Agycode.rb > validates_uniqueness_of *:descr*, > *:scope* => "project_id" > > Here''s the test that SHOULDN''T pass but it does > > *def* test_should_fail_duplicate_descr > record1 = *@project*.agycodes.*new*(*:descr* => "description") > record2 = record1 > assert record1.save, "Save Record" > assert_valid(record1) > assert record2.save, "Save Record" > assert_valid(record2) > *end* > > By the way, do you know how one would write the opposite of "assert_valid"? > Thank you, > David--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Seemingly Similar Threads
- Finding distinct months using find_by_sql
- assert_valid in unit tests after upgrade to rails 2.3 doesn't work
- Saving the monitor file on new file always using Monitor(wav, Record1, m)
- 3 columns
- problem when exiting from "record file" function without pressing the escape digit