# ActiveRecord Unique key association

**URL:** <https://www.ruby-forum.com/t/activerecord-unique-key-association/87272>\
**Category:** Rails\
**Created:** [March 1, 2007, 11:49am UTC](https://www.ruby-forum.com/t/activerecord-unique-key-association/87272 "2007-03-01T11:49:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![akm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@akm](https://www.ruby-forum.com/u/akm)\
**Post date:** [March 1, 2007, 11:49am UTC](https://www.ruby-forum.com/t/activerecord-unique-key-association/87272/1 "2007-03-01T11:49:57Z")

</div>

Hi,  
I am in the process of incorporating a legacy database into my first  
ruby/rails project. All is going well so far (he says crossing all  
toes/fingers!).

I have a User tbl which has a unique key association on two columns,  
username and userpassword. So far I have:

class Usertbl \< ActiveRecord::Base  
set\_primary\_key “user\_id”  
end

and I want to add a line after the primary\_key declaration, something  
like:

set\_unique\_key “username, userpassword”

How would I do this?

–  
Regards

Andrew

---

<div class="post-metadata">

**Author:** ![akm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@akm](https://www.ruby-forum.com/u/akm)\
**Post date:** [March 1, 2007, 12:31pm UTC](https://www.ruby-forum.com/t/activerecord-unique-key-association/87272/2 "2007-03-01T12:31:06Z")

</div>

Hi Andrew

You need something along the lines of

validates\_uniqueness\_of :username, :password

I think

I am still fairly new at this myself!

---

<div class="post-metadata">

**Author:** ![akm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@akm](https://www.ruby-forum.com/u/akm)\
**Post date:** [March 1, 2007, 1:20pm UTC](https://www.ruby-forum.com/t/activerecord-unique-key-association/87272/3 "2007-03-01T13:20:17Z")

</div>

Hi Rod,

validates\_uniqueness\_of :username, :password

I think you have it. What I have done is:

validates\_uniqueness\_of :username,:userpassword, :scope =\> :user\_id

which I believe means, there can only be one username/userpassword  
combination for any given user\_id? If one of you more knowledgeable  
chaps  
would like to chime in at this juncture, that would be appreciated 😉

–  
Regards

Andrew

---

<div class="post-metadata">

**Author:** ![akm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@akm](https://www.ruby-forum.com/u/akm)\
**Post date:** [March 1, 2007, 1:42pm UTC](https://www.ruby-forum.com/t/activerecord-unique-key-association/87272/4 "2007-03-01T13:42:19Z")

</div>

i don’t see why you would want a unique combination of username AND  
password. just a unique key on username should suffice. if you’ve  
got one on both, that would allow two users could have the same  
username with different passwords and which would satisfy the unique  
restriction, and that is something you don’t want.

ie,

username unique =\> good  
(username, password) unique =\> bad

ex:

mysql\> create table testusers (  
-\> id int auto\_increment,  
-\> username varchar(20),  
-\> password varchar(20),  
-\> primary key(id),  
-\> unique username\_password (username, password));

so i have a unique index on the username AND password combination.

mysql\> insert into testusers values (’’, ‘bob’, ‘bobpw1’);  
Query OK, 1 row affected, 1 warning (0.10 sec)

mysql\> insert into testusers values (’’, ‘bob’, ‘bobpw2’);  
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql\> select \* from testusers;  
±—±---------±---------+  
| id | username | password |  
±—±---------±---------+  
| 1 | bob | bobpw1 |  
| 2 | bob | bobpw2 |  
±—±---------±---------+  
2 rows in set (0.00 sec)

now, if i just make username unique by itself:

mysql\> insert into testusers values (’’, ‘bob’, ‘bobpw1’);  
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql\> insert into testusers values (’’, ‘bob’, ‘bobpw2’);  
ERROR 1062 (23000): Duplicate entry ‘bob’ for key 2

---

<div class="post-metadata">

**Author:** ![akm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@akm](https://www.ruby-forum.com/u/akm)\
**Post date:** [March 1, 2007, 2:31pm UTC](https://www.ruby-forum.com/t/activerecord-unique-key-association/87272/5 "2007-03-01T14:31:28Z")

</div>

Hi Chris,

username unique =\> good

> (username, password) unique =\> bad

Yes, you are correct! So noted and amended. Thanks.

–  
Regards

Andrew
