This project is archived and is in readonly mode.
rails3: ActiveRecord::AutosaveAssociation.validate_single_association being triggered when it shouldn't
Reported by John G. | November 11th, 2010 @ 06:11 PM
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 ofPostsController.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
- It is being called on an instance of
- The third call:
- It is being called on a instance of
User
, notPost
- it is being called via
ActiveRecord::Validations
-
ActiveRecord::AutosaveAssociation.validate_single_association
ActiveModel::Validations
special_msg
is blank (nil
)
- It is being called on a instance of
- The first and second calls:
I've actually posted this question on Stackoverflow as well, but so far, no answers.
Comments and changes to this ticket
-
Santiago Pastorino February 11th, 2011 @ 07:18 PM
- 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.
-
Santiago Pastorino February 11th, 2011 @ 07:18 PM
- State changed from open to stale
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>