Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

[PATCH] AutosaveAssociation generates unneeded validations

#3161

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.

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

State: resolved
Milestone: none
Assigned to: Eloy Duran Eloy Duran
Importance: none

Activity

  1. Alexey Kovyrin
  2. Dmytro Shteflyuk
    Dmytro Shteflyuk

    Alexey, thank you for your great work. I think this is serious problem and performance degradation is not acceptable. Some of our actions performed more than 2 times slower than in Rails 2.1.

    Hope, core team will accept your patch in a short time, until then we will use patched Rails.

    September 7th, 2009 @ 10:39 PM

  3. Eloy Duran
    Eloy Duran

    The patch looks great Alexey, thanks! I'll take some time later this week to apply to my branch and rebaseetc, so I can get the whole branch applied upstream.

    September 9th, 2009 @ 09:43 AM

  4. Repository
  5. Repository
  6. Tom Stuart
    Tom Stuart

    This change breaks validation of autosave associations in some circumstances. See #3355 Validation of autosaved associations is broken.

    October 8th, 2009 @ 04:41 PM