Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

after_callback is not called on nested resources #22943

Closed
xdmx opened this issue Jan 6, 2016 · 8 comments
Closed

after_callback is not called on nested resources #22943

xdmx opened this issue Jan 6, 2016 · 8 comments

Comments

@xdmx
Copy link

xdmx commented Jan 6, 2016

I'm using rails 4.2.5 and I've noticed that after_callback is not called on nested resources

class Post < ActiveRecord::Base
  belongs_to :user
  after_rollback { puts 'nested' }
end 

class User < ActiveRecord::Base
  has_many :posts
  accepts_nested_attributes_for :posts
  after_rollback { puts 'top level' }
end

When a user is created passing posts_attributes, it only puts top level, but not nested

In my opinion it should also call the nested rollback, since it could be do something important

@pixeltrix
Copy link
Contributor

@xdmx can you check whether the records have been saved to the database before the rollback occurs. I think what's happening is that the nested records are only in memory and so haven't been added to the transaction yet.

@xdmx
Copy link
Author

xdmx commented Jan 7, 2016

@pixeltrix from the rails log I see only SELECT queries called by validations before and after the ROLLBACK, but none with INSERT or UPDATE

So far I've solved it with the following:

class Post < ActiveRecord::Base
  belongs_to :user
  def manual_rollback
    puts 'nested'
  end
end 

class User < ActiveRecord::Base
  has_many :posts
  accepts_nested_attributes_for :posts
  after_rollback { posts.each(&:manual_rollback) }
end

Which is not the best way but it does the work

@pixeltrix
Copy link
Contributor

@xdmx was there an insert/update for the top level model?

@xdmx
Copy link
Author

xdmx commented Jan 7, 2016

nope, only selects in the log after running (which failed some validations on the nested record)

user = User.first
user.posts_attributes = [{title: 'foo'}]
user.save

@pixeltrix
Copy link
Contributor

@xdmx can you make it so the top level model passes validation and then the transaction fails because of a validation failure on a nested model. Also what database are you using?

@xdmx
Copy link
Author

xdmx commented Jan 7, 2016

Yup, it was already like that, the top level model (user) was valid, and the nested model (post) was not

I'm using postgresql 9.4.5 under Arch linux

@rails-bot
Copy link

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 team are limited, and so we are asking for your help.

If you can still reproduce this error on the 5-0-stable branch or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot
Copy link

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 5-0-stable branch or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants