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.

Find on nested models does not trigger callbacks

#5925

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!

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

State: resolved
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • 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
    

    November 10th, 2010 @ 09:29 PM

  2. Matthias Kühnert
    Matthias Kühnert

    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.

    November 13th, 2010 @ 01:46 PM

  3. Neeraj Singh
    Neeraj Singh
    • State changed from new to resolved

    November 23rd, 2010 @ 09:27 PM