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.

This project is archived and is in readonly mode.

rails3: ActiveRecord::AutosaveAssociation.validate_single_association being triggered when it shouldn't

#5955

I'm noticing Rails call ActiveRecord::AutosaveAssociation.validate_single_association when I save a model that belongs to another, though it shouldn't be according to the documentation. The documentation says that this method will be called when :validate or :autosave is turned on for an association, and I haven't done that.

This is causing me an issue because of the way I have set up my models and the actions that are performed before validation occurs. Let's take for example a User and Post model.

  • A user has many posts.
  • A post belongs to a user.

abstract_model.rb

class AbstractModel < ActiveRecord::Base
  self.abstract_class = true
  before_validation :foo
  attr_accessor :special_msg

  def foo
    puts special_msg
  end
end

user.rb

class User < AbstractModel
   has_many :posts
end

post.rb

class Post < AbstractModel
  belongs_to :user
end

Notice that I haven't defined :validate or :autosave on the association.

PostsController.update

def update
  @post = Post.find(params[:id]
  @post.special_msg = "Dumbledore dies, so does Hedwig"
  @post.update_attributes(params[:post])
end

Using the debugger, I noticed the following:

  • AbstractModel.foo gets called 3 times from the last line of PostsController.update
    • The first and second calls:
      • It is being called on an instance of Post
      • It is being called via
        • ActiveRecord::Validations
        • ActiveModel::Validations
      • special_msg is not blank
    • The third call:
      • It is being called on a instance of User, not Post
      • it is being called via
        • ActiveRecord::Validations
        • ActiveRecord::AutosaveAssociation.validate_single_association
        • ActiveModel::Validations
      • special_msg is blank (nil)

I've actually posted this question on Stackoverflow as well, but so far, no answers.

Reported by John G. · November 11th, 2010 @ 06:11 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 11th, 2011 @ 07:18 PM

  2. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 11th, 2011 @ 07:18 PM