# Validates\_uniqueness\_of on a facade column

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-on-a-facade-column/115419>\
**Category:** Rails\
**Created:** [September 25, 2007, 3:46am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-on-a-facade-column/115419 "2007-09-25T03:46:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [September 25, 2007, 3:46am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-on-a-facade-column/115419/1 "2007-09-25T03:46:26Z")

</div>

Hey all…

I’ve got a facade column set up (specifically, I’ve got a column in my  
database called ip\_address that stores an IP as a 32-bit integer rather  
than the dotted notation, but the model reads and writes as the dotted  
notation), but I want to validate that the IP is unique in the database  
whenever an entry is created or edited.

is there any way to do this?

simply putting ‘validates\_uniqueness\_of :ip\_address’ doesn’t work since  
it’s querying my database with the dotted notation rather than the  
32-bit integer value.

thanks!

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [September 25, 2007, 4:08am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-on-a-facade-column/115419/2 "2007-09-25T04:08:57Z")

</div>

You may have to roll your own like:

def before\_save  
find(:all, :conditions =\> [‘ip\_address = ?’,  
convert\_to\_int(self.ip\_address)]).empty?  
end

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [September 25, 2007, 4:13am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-on-a-facade-column/115419/3 "2007-09-25T04:13:58Z")

</div>

just a note, convert\_to\_int is simply a facade that you should replace  
with whatever method you are using to do the conversion, I assume  
IPAddr#to\_i, but none-the-less, you get the gist of it.

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [September 25, 2007, 4:32am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-on-a-facade-column/115419/4 "2007-09-25T04:32:14Z")

</div>

I’m putting it in my validate function since I want to be able to report  
errors to the user.

and it works… awesome, thanks!

I didn’t even think of doing it like that.

my complete function is:

def validate

# check if the ip\_address is unique

@s = Machine.find(:all, :conditions =\> [‘ip\_address = ? and id \<\> ?’,  
IPAddress.to\_int(self.ip\_address), self.id])

if @s.length \> 0  
errors.add :ip\_address, “is already taken”  
end

end

looks alright, right?

---

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [September 25, 2007, 4:39am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-on-a-facade-column/115419/5 "2007-09-25T04:39:00Z")

</div>

yep, or you could clean it up even more by eliminating the unnecessary  
instance variable:

def validate

# check if the ip\_address is unique

unless Machine.find(:all, :conditions =\> [‘ip\_address = ? and id \<\> ?’,  
IPAddress.to\_int(self.ip\_address), self.id]).empty?  
errors.add :ip\_address, “is already taken”  
end  
end

However, both will work, this one just uses a tad less memory. Glad it  
worked for ya.
