# Validates\_length\_of virtual attribute

**URL:** <https://www.ruby-forum.com/t/validates-length-of-virtual-attribute/145535>\
**Category:** Rails\
**Created:** [August 11, 2008, 10:37am UTC](https://www.ruby-forum.com/t/validates-length-of-virtual-attribute/145535 "2008-08-11T10:37:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [August 11, 2008, 10:37am UTC](https://www.ruby-forum.com/t/validates-length-of-virtual-attribute/145535/1 "2008-08-11T10:37:15Z")

</div>

Hi all,

I have in my Person model a attribute named “hash” which is a hash of a  
password and a salt.  
When a user want to create a account of update it, he just have to  
specify its password in a password virtual attribute. To do so, I use  
this code:

# in model

class Person \< ActiveRecord::Base  
validates\_length\_of :password, :within =\> 6…40  
def password  
end  
end

# in view

Password \<%= f.password\_field :password %\>

But when I try to create a account for testing, I get an error message  
from the model that said my password is so short (less then 6 chars)…  
even if my password got more chars. I don’t understand why.

Thanks for any help.  
D.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [August 11, 2008, 11:35am UTC](https://www.ruby-forum.com/t/validates-length-of-virtual-attribute/145535/2 "2008-08-11T11:35:52Z")

</div>

On 11 Aug 2008, at 09:37, David B. wrote:

> # in model
> 
> from the model that said my password is so short (less then 6  
> chars)…  
> even if my password got more chars. I don’t understand why.

Think about what happens: the validation runs, and tries to get the  
password to see if you’ve set it to at least 6 characters. It calls  
the password method, which returns nil so the validation fails. Your  
password accessor needs to return the value set by the form (you could  
just use attr\_accessor).

Fred

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [August 11, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/validates-length-of-virtual-attribute/145535/3 "2008-08-11T22:07:50Z")

</div>

Frederick C. wrote:

> Think about what happens: the validation runs, and tries to get the  
> password to see if you’ve set it to at least 6 characters. It calls  
> the password method, which returns nil so the validation fails. Your  
> password accessor needs to return the value set by the form (you could  
> just use attr\_accessor).
> 
> Fred

Thank you for your answer. By using attr\_accessor :password, my password  
attribute can be read and write.

But I have a thin other problem because of my special write attribute:

def password=(password)  
self.salt = salt = Digest::SHA1.hexdigest(Time.now.to\_s)  
self.shadow = Digest::SHA1.hexdigest(password + salt)  
end

Do you think there is a way to solve it by modify this method and just  
remplace attr\_accessor :password by attr\_reader :password?

Thanks again for your help.  
D.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [August 11, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/validates-length-of-virtual-attribute/145535/4 "2008-08-11T22:43:16Z")

</div>

On 11 Aug 2008, at 21:07, David B. wrote:

> Do you think there is a way to solve it by modify this method and just  
> remplace attr\_accessor :password by attr\_reader :password?

Well you need the password method to work, so something like

def password=(password)  
@password = password

# rest of your method here

end

Fred

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [August 11, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/validates-length-of-virtual-attribute/145535/5 "2008-08-11T22:58:33Z")

</div>

Frederick C. wrote:

> def password=(password)  
> @password = password
> 
> # rest of your method here
> 
> end
> 
> Fred

Oops, I haven’t thinking of this… Now that’s okay,  
Thanks a lot!
