This project is archived and is in readonly mode.

#5925 ✓resolved
Matthias Kühnert

Find on nested models does not trigger callbacks

Reported by Matthias Kühnert | November 6th, 2010 @ 11:56 AM

Rails 3.0.1: find on nested models does not trigger callbacks:

class TeachingGroup < ActiveRecord::Base
  belongs_to :school_term
  ...
  after_update :foo
end

In TeachingGroupsController, any callbacks on the models are not triggered when I use a nested find. The model returned is the same in each case:

class TeachingGroupsController < ApplicationController
  ...
  def update
    # this works, triggers callbacks on update_attributes or create:
    @teaching_group = TeachingGroup.find(params[:id])
    
    # for security, I want this, but this DOES NOT trigger callbacks:
    @teaching_group = @current_term.teaching_groups.find(params[:id])
     
    # works, triggers callbacks
    @teaching_group = TeachingGroup.find_by_id_and_school_term_id(params[:id], @current_term.id)

    if @teaching_group.update_attributes( params[:teaching_group] ) ...
    ...
  end
  ...
end

Same phenomenon in create. The main problem is that the cache sweeper does not work either.
Please help, and many thanks for your hard work on Rails!

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh November 10th, 2010 @ 09:29 PM

    • Importance changed from “” to “Low”

    Not sure what you mean by NestedModel since I did not see any NestedModel in the example you illustrated.

    Following code works for me with rails edge.

    class Car < ActiveRecord::Base
      has_many :brakes
    end
    class Brake < ActiveRecord::Base
      belongs_to :car
    
      after_update :foo
    
      def foo
        puts 'foo'*50
      end
    
      def self.lab
        Car.create(:name => 'honda') unless Car.first
        Car.first.brakes.create(:name => 'b1') unless Brake.first
    
        b = Brake.find(1)
        b.update_attributes({:name => 'b2'})
    
        b = Car.first.brakes.find(1)
        b.update_attributes({:name => 'b3'})
    
        b = Brake.find_by_id_and_name(1, 'b3')
        b.update_attributes({:name => 'b4'})
      end
    
    end
    
  • Matthias Kühnert

    Matthias Kühnert November 13th, 2010 @ 01:46 PM

    OK, I have just found this error does not occur when I use "rails server", but when I use passenger 3.0.0. Sorry, these are not nested models but simple has_many relations.

  • Neeraj Singh

    Neeraj Singh November 23rd, 2010 @ 09:27 PM

    • State changed from “new” to “resolved”

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>

Pages