Forum: Ruby on Rails Model validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E4e9e36d6730fd5a99e56ec4f53172ff?d=identicon&s=25 Dowker (Guest)
on 2009-01-23 20:22
(Received via mailing list)
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.
B54c73767addd2a75cf005e3941b7829?d=identicon&s=25 Aldo Sarmiento (sarmiena)
on 2009-01-23 21:49
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 Sarmiento

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.
B54c73767addd2a75cf005e3941b7829?d=identicon&s=25 Aldo Sarmiento (sarmiena)
on 2009-01-23 21:53
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 Sarmiento

Aldo Sarmiento 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 Sarmiento
>
> 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.
E4e9e36d6730fd5a99e56ec4f53172ff?d=identicon&s=25 Dowker (Guest)
on 2009-01-23 22:20
(Received via mailing list)
Thanks Aldo. That was exactly what I was trying to communicate. Sorry
if I was not clearer.


On Jan 23, 3:49 pm, Aldo Sarmiento <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.