# Limiting scope of :validates\_uniqueness\_of

**URL:** <https://www.ruby-forum.com/t/limiting-scope-of-validates-uniqueness-of/193557>\
**Category:** Rails\
**Created:** [August 19, 2010, 5:23pm UTC](https://www.ruby-forum.com/t/limiting-scope-of-validates-uniqueness-of/193557 "2010-08-19T17:23:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [August 19, 2010, 5:23pm UTC](https://www.ruby-forum.com/t/limiting-scope-of-validates-uniqueness-of/193557/1 "2010-08-19T17:23:06Z")

</div>

I’m having a hard time limiting the scope of new comments to the Post  
model. So, for validates\_uniqueness\_of :title in the comment.rb model,  
that uniqueness would only be attached to the post I’m commenting to,  
not globally among all comments in the application.

I tried putting this in comment.rb:

validates\_uniqueness\_of :title, :scope =\> :post

but got this error message in my terminal:

Processing CommentsController#create (for 127.0.0.1 at 2010-08-16  
21:28:32) [POST]  
Parameters: {“comment”=\>{“title”=\>“rent”}, “commit”=\>“Submit Your  
Comment”,  
“authenticity\_token”=\>“z5AzsFgaJPdvGiTyy3Yeyb8f0RV+dmpdw5PanAadjOw=”,  
“game\_id”=\>“5”}  
Post Load (0.2ms) SELECT \* FROM “posts” WHERE (“posts”.“id” = 5)  
CACHE (0.0ms) SELECT \* FROM “posts” WHERE (“posts”.“id” = 5)  
Response Load (0.0ms) SQLite3::SQLException: no such column:  
comments.posts: SELECT “comments”.id FROM “comments” WHERE  
(“comments”.“title” = ‘rent’ AND “comments”.post = 5) LIMIT 1

ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column:  
comments.post: SELECT “comments”.id FROM “comments” WHERE  
(“comments”.“title” = ‘rent’ AND “comments”.post = 5) LIMIT 1):  
app/controllers/comments\_controller.rb:4:in `create’

Rendered rescues/\_trace (110.5ms)  
Rendered rescues/\_request\_and\_response (1.2ms)  
Rendering rescues/layout (internal\_server\_error)

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [August 19, 2010, 5:45pm UTC](https://www.ruby-forum.com/t/limiting-scope-of-validates-uniqueness-of/193557/2 "2010-08-19T17:45:14Z")

</div>

On Aug 19, 2010, at 11:23 AM, Ze Ca wrote:

> I’m having a hard time limiting the scope of new comments to the Post  
> model. So, for validates\_uniqueness\_of :title in the comment.rb model,  
> that uniqueness would only be attached to the post I’m commenting to,  
> not globally among all comments in the application.
> 
> I tried putting this in comment.rb:
> 
> validates\_uniqueness\_of :title, :scope =\> :post

:scope =\> :post\_id

> CACHE (0.0ms) SELECT \* FROM “posts” WHERE (“posts”.“id” = 5)  
> Rendered rescues/\_request\_and\_response (1.2ms)  
> .  
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en)  
> .

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [August 19, 2010, 9:30pm UTC](https://www.ruby-forum.com/t/limiting-scope-of-validates-uniqueness-of/193557/3 "2010-08-19T21:30:22Z")

</div>

Ah, ok. That did it. I see now I would want to limit the scope to the  
post\_id column, instead of the post table.

Thank you!

Rob B. wrote:

> On Aug 19, 2010, at 11:23 AM, Ze Ca wrote:
> 
> > I’m having a hard time limiting the scope of new comments to the Post  
> > model. So, for validates\_uniqueness\_of :title in the comment.rb model,  
> > that uniqueness would only be attached to the post I’m commenting to,  
> > not globally among all comments in the application.
> > 
> > I tried putting this in comment.rb:
> > 
> > validates\_uniqueness\_of :title, :scope =\> :post
> 
> :scope =\> :post\_id
> 
> > CACHE (0.0ms) SELECT \* FROM “posts” WHERE (“posts”.“id” = 5)  
> > Rendered rescues/\_request\_and\_response (1.2ms)  
> > .  
> > For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en)  
> > .
> 
> Rob B.  
> removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
> removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)
