Default value for attribute in model

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

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.

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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs