model -> model validations

If I have a site model that has_many :equipments, how would I validate the equipment only when ceratin conditions exist in the site model?

I add the equipment dynamically in the Site form and also the form will not necessarily be "fully submitted" until some conditions exist in the main site form.

This maybe of some help:

Cheers, Nicholas

Well sort of. I really need to validate another model based on the original form(model) conditions.

Well sort of. I really need to validate another model based on the original form(model) conditions.

Do you mean you are trying to validate a model that has already been written to the database, when a form for a different model is submitted? If not I think a little more detail in exactly what you are trying to do may help.

Colin

Yes, Site is the main model, equipment is associated. I only want to validate the equipment model when conditions exist in the site model.

Yes, Site is the main model, equipment is associated. I only want to validate the equipment model when conditions exist in the site model.

Is that 'yes' in response to ' Do you mean you are trying to validate a model that has already been written to the database'? If so then that does not make sense. Validation is about checking that data is correct before allowing it to be written to the db. If it is already in the db it is too late to validate it.

Colin

Well it may not seem like it does not make sense but in the context of this app it does with the what the form is supposed .

This is our flow.

  1. Site is installed with eqpt.

  2. The person using the form puts all eqpt that is on site attached to the form dynamically.

  3. I have fields for the eqpt showing that it is installed correctly, settings are correct , …

  4. You can add eqpt but is might not necessarily be installed correctly, have issues, blah…

Step 4 is the reason that the form may be “submitted” but not fully processed until the issues at a site get resolved.

Well it may not seem like it does not make sense but in the context of this app it does with the what the form is supposed .

Pardon?

This is our flow. 1. Site is installed with eqpt. 2. The person using the form puts all eqpt that is on site attached to the form dynamically. 3. I have fields for the eqpt showing that it is installed correctly, settings are correct , .... 4. You can add eqpt but is might not necessarily be installed correctly, have issues, blah.... Step 4 is the reason that the form may be "submitted" but not fully processed until the issues at a site get resolved.

No, sorry, I am completely flummoxed now, I have no idea what you are trying to do. You are not explaining the problem in terms that I understand, which may well be my fault.

Colin

I have the main form “Site”, has many :equipments.

In the Site form I add equipment to the form dynamically to associate it to the site.

I do not necessarily need to validate the equipment fields on the form submission unless a checkbox is checked on the “Site” form stating everything is good.

Basically I only need to validate the equipment under specific case on the site model.

So, is this it? A user has entered data for an Equipment record onto the form and submitted it, you have some validations defined but you only want to run them if the associated site record (in the db) has a particular flag set? If the flag in the db is not set then you want to save the Equipment record without validating?

Colin

Ya, now that you said a flag would be good. I was initially referring to the main Site model initiating the validation for the equipment but a flag in the equipment model would probably be an easier solution.

Hello Chris:

There's also an :if option for the validates_associated class method:

":if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value."

http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_associated

Cheers, Nicholas

I think from your description you don’t really want ‘validations’ in the rails sense. You might want to look into using some sort of state machine and do some conditions checking of the interrelated models when the main model is saved.