# Default value for attribute in model

**URL:** <https://www.ruby-forum.com/t/default-value-for-attribute-in-model/70849>\
**Category:** Rails\
**Created:** [October 3, 2006, 1:58pm UTC](https://www.ruby-forum.com/t/default-value-for-attribute-in-model/70849 "2006-10-03T13:58:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_NoSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@Nicolas\_NoSSSS](https://www.ruby-forum.com/u/Nicolas_NoSSSS)\
**Post date:** [October 3, 2006, 1:58pm UTC](https://www.ruby-forum.com/t/default-value-for-attribute-in-model/70849/1 "2006-10-03T13:58:19Z")

</div>

Hi,

Here is my little question :

I’d like to have an attribute in my model class wich is always at true,  
except when I declare it at false. For the moment I have declared my  
accessor with

attr\_accessor :allow\_validation

and put a default value by redifining initialize :

def initialize (params=nil)  
super(params)  
@allow\_validation = true  
end

Of course, I can now have objects constructed without calling  
initialize, for example if the object is created using the find method  
of the parent class. Is there a clean way to do thaht without repeating  
myself and redifining all methods from the parent class tht could create  
an object of thus type ?

Thanks in advance,

Nicolas

---

<div class="post-metadata">

**Author:** ![Nicolas\_NoSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@Nicolas\_NoSSSS](https://www.ruby-forum.com/u/Nicolas_NoSSSS)\
**Post date:** [October 3, 2006, 2:39pm UTC](https://www.ruby-forum.com/t/default-value-for-attribute-in-model/70849/2 "2006-10-03T14:39:37Z")

</div>

On 3 October 2006 15:58, Nicolas NoÃ© wrote:

> and put a default value by redifining initialize :  
> an object of thus type ?  
> If you want to run some code after the record was found in DB, take a  
> look at  
> after\_find and after\_initialize callbacks.

class MyClass  
def initialize (params=nil)  
super(params)  
@allow\_validation = true  
end

def after\_initialize  
@allow\_validation = true  
end  
end

But I would recommend to use some other method to avoid validation.

---

<div class="post-metadata">

**Author:** ![Nicolas\_NoSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@Nicolas\_NoSSSS](https://www.ruby-forum.com/u/Nicolas_NoSSSS)\
**Post date:** [October 3, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/default-value-for-attribute-in-model/70849/3 "2006-10-03T17:05:56Z")

</div>

On 3 October 2006 18:42, Nicolas NoÃ© wrote:

> The problem is with this third action : I just cannot redisplay password  
> in a hidden or non editable field, because it’s stored as a hash in the  
> database.
> 
> What do you suggest for that ? (I’m new in rails, and find it great. But  
> not so easy to find the “good way” to solve a problem).  
> Take a look at Acts As Authenticated plugin and Login Engine. Those  
> should  
> contain ready solutions for that problem.

---

<div class="post-metadata">

**Author:** ![Nicolas\_NoSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@Nicolas\_NoSSSS](https://www.ruby-forum.com/u/Nicolas_NoSSSS)\
**Post date:** [October 3, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/default-value-for-attribute-in-model/70849/4 "2006-10-03T16:42:16Z")

</div>

Maxim K. wrote:

> On 3 October 2006 15:58, Nicolas NoÃ© wrote:
> 
> > and put a default value by redifining initialize :  
> > an object of thus type ?  
> > If you want to run some code after the record was found in DB, take a  
> > look at  
> > after\_find and after\_initialize callbacks.
> 
> class MyClass  
> def initialize (params=nil)  
> super(params)  
> @allow\_validation = true  
> end
> 
> def after\_initialize  
> @allow\_validation = true  
> end  
> end
> 
> But I would recommend to use some other method to avoid validation.

Thanks a lot !

What would you recommend for avoiding validation ?

In fact, I have a User model, and 3 actions :

1. Add an user : I have to perform all validations of a model
2. Change password : I have to validate password only, but I can execute  
all validations : the others will succeed because the User is already  
valid (it was checked when adding)
3. Edit a user : this action is special : I edit the user, except the  
field username (I don’t want user to edit it) and password (changing  
password is done by action 2) ).

The problem is with this third action : I just cannot redisplay password  
in a hidden or non editable field, because it’s stored as a hash in the  
database.

I first created the User class with the validation for 3, and then  
created a subclass that also perform validation for 1 and 2. That  
worked, but the code was quite long for a so simple problem, and I had  
some problems when using ruby-gettext to translate model error messages.  
That’s why I wanted to solve my problem by having only one user class,  
that performs alll validations excepts if we specify  
@user.allow\_validation = false in the controller.

What do you suggest for that ? (I’m new in rails, and find it great. But  
not so easy to find the “good way” to solve a problem).

Thanks

Nicolas
