Model validation

I need a little advice on how to best set things up. I have a model
where I will have a short form and a longer form in two different
places in my application. Obviously, I would like to apply validating
rules to both forms. The short form is a subset of the fields in the
model. The long form is the full model so it isn’t a problem. What are
my options?

(1) There is functionality in the models that I don’t know about to
handle this situation.
(2) I should create a second model for the extra fields between the
short and long forms, using the same database table.
(3) I should create a second model for the extra fields between the
short and long forms, using a different database table and a has_one
association.
(4) Any other suggestions?

Thanks.

It’s kind of unclear about what you are trying to do here. Say you have
a model like so:

class Customer < ActiveRecord::Base
# contains attributes :fname, :lname, :phone, :city, :state, :zip
validates_presence_of :fname, :lname, :phone, :zip
end

This model will validate the presence of the 4 fields (:fname, :lname,
:phone, :zip). Would this be comparable to the “longer” version?

If so, would the shorter version be required to ONLY validate a subset
of the validates_presence_of list? Perhaps only :fname & :lname?

If this is the case you can just set the default validations to the
short list like so:

validates_presence_of :fname, :lname

In order to get the long validation, you can do this with your model:

class Customer < ActiveRecord::Base
# contains attributes :fname, :lname, :phone, :city, :state, :zip
validates_presence_of :fname, :lname

@long_validation
attr_accessor :long_validation

def validate
    if @long_validation
        errors.add("phone number cannot be blank") if phone.blank?
        errors.add("zip code cannot be blank") if zip.blank?
    end
end

end

Test this using your script/console from the terminal.

HTH,
Aldo S.

Dowker wrote:

I need a little advice on how to best set things up. I have a model
where I will have a short form and a longer form in two different
places in my application. Obviously, I would like to apply validating
rules to both forms. The short form is a subset of the fields in the
model. The long form is the full model so it isn’t a problem. What are
my options?

(1) There is functionality in the models that I don’t know about to
handle this situation.
(2) I should create a second model for the extra fields between the
short and long forms, using the same database table.
(3) I should create a second model for the extra fields between the
short and long forms, using a different database table and a has_one
association.
(4) Any other suggestions?

Thanks.

Ack!

Forgot to mention, that now you can do this in the controller when
loading the ActiveRecord object:

def some_action
@customer = Customer.find_by_id(1) #however you are loading the
object
@customer.long_validation = true
# now you can use @customer in your form_for @customer in the view!
end

HTH,
Aldo S.

Aldo S. wrote:

It’s kind of unclear about what you are trying to do here. Say you have
a model like so:

class Customer < ActiveRecord::Base
# contains attributes :fname, :lname, :phone, :city, :state, :zip
validates_presence_of :fname, :lname, :phone, :zip
end

This model will validate the presence of the 4 fields (:fname, :lname,
:phone, :zip). Would this be comparable to the “longer” version?

If so, would the shorter version be required to ONLY validate a subset
of the validates_presence_of list? Perhaps only :fname & :lname?

If this is the case you can just set the default validations to the
short list like so:

validates_presence_of :fname, :lname

In order to get the long validation, you can do this with your model:

class Customer < ActiveRecord::Base
# contains attributes :fname, :lname, :phone, :city, :state, :zip
validates_presence_of :fname, :lname

@long_validation
attr_accessor :long_validation

def validate
    if @long_validation
        errors.add("phone number cannot be blank") if phone.blank?
        errors.add("zip code cannot be blank") if zip.blank?
    end
end

end

Test this using your script/console from the terminal.

HTH,
Aldo S.

Dowker wrote:

I need a little advice on how to best set things up. I have a model
where I will have a short form and a longer form in two different
places in my application. Obviously, I would like to apply validating
rules to both forms. The short form is a subset of the fields in the
model. The long form is the full model so it isn’t a problem. What are
my options?

(1) There is functionality in the models that I don’t know about to
handle this situation.
(2) I should create a second model for the extra fields between the
short and long forms, using the same database table.
(3) I should create a second model for the extra fields between the
short and long forms, using a different database table and a has_one
association.
(4) Any other suggestions?

Thanks.

Thanks Aldo. That was exactly what I was trying to communicate. Sorry
if I was not clearer.

On Jan 23, 3:49 pm, Aldo S. [email protected]