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
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 = => “description”)
record2 = record1
assert, “Save Record”
assert, “Save Record”

By the way, do you know how one would write the opposite of
Thank you,

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs