This project is archived and is in readonly mode.

#3161 ✓resolved
Alexey Kovyrin

[PATCH] AutosaveAssociation generates unneeded validations

Reported by Alexey Kovyrin | September 7th, 2009 @ 08:59 PM

For models with many (tens, hundreds) associations Rails after 5cda000b would generate tons of dummy validation methods (one per association) that would call validate_collection_association/validate_single_association methods to figure out if any validations needed at all.

In our project with hundreds of highly-relational models (some models have 50+ associations) this behavior causes the application code to work significantly slower (tests execution time x 1.5, some controllers actions work 4x slower).

Aside from the performance degradation there is another problem: according to official Rails documentation, there are few different cases in association validations behavior:

  1. has_many associations are validated by default (:validate option is true by default)
  2. all other associations have :validate set to false by default
  3. any association could be switched to validation mode by adding :validate => true to the declaration

In my patch and tests I've changed the way Rails generate validation methods to make it work in accordingly. If an association does not need to be validated, there is no need to generate a method for it and call this callback method for each validation.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages