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.

attr_readonly not set when counter_cache set to true

#34

Due to the class loading feature in Rails, a model class with counter_cache could be loaded before its associated class, which may cause attr_readonly for "[model]_count" field not set for its associated class.

The chances of this occuring in development environment is much higher as codes are reloaded for every request.

A simple test case to trigger this issue:

class Topic
end

class Post
  belongs_to :topic, :counter_cache => true  
end

class CounterCacheAttrReadonlyTest < Test::Unit::TestCase
  def test_attr_readonly_not_set
    Post.create :topic_id => 1
    
    # Failed, attr_readonly for posts_count in Topic not set 
    # because Topic has not been loaded and defined? returns false      
    assert_equal Set.new('posts_count'), Topic.attr_readonly 
  end
end

Attached is the patch. Thanks.

Reported by Herryanto Siatono · April 23rd, 2008 @ 04:02 AM

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

Activity

  1. Michael Koziarski
    Michael Koziarski

    Could you add the tests to the .patch you've attached please? Two style issues

    
    if (reflection.klass rescue nil) and reflection.klass.respond_to?(:attr_readonly)
    
    

    Why do you need the rescue there, and trailing conditionals shouldn't be that long, rewrite it to if ... end instead.

    Cheers!

    April 23rd, 2008 @ 09:15 AM

  2. DHH
    DHH
    • State changed from new to incomplete

    Change ticket status to open when fixed.

    April 29th, 2008 @ 11:20 PM

  3. Rohit Arondekar
    Rohit Arondekar
    • State changed from incomplete to stale
    • Tag set to activerecord, patch
    • Importance changed from to

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

    October 6th, 2010 @ 06:47 AM