Forum: Ruby on Rails bug? found by way of counter_cache/default bug

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
sanedragon@gmail.com (Guest)
on 2007-01-18 12:00
(Received via mailing list)
I decided that I wanted tags with counts on my custom rails blog
(shameless plug: http://rorek.org/blog/ ), so I perhaps ill-advisedly
commenced editing the acts_as_taggable plugin in place.  I ran up
against a bug: the :default for the taggings_count field apparently
wasn't working, and was set to nil for new tags.  Continuing on my
spate of perhaps ill-advised behavior, I decided to try to work around
the problem like so (with extra p's to display the weird behavior):

added to vendor/plugins/acts_as_taggable/tag.rb:
  def before_save
    # XXX I should really find out why :default doesn't work
    if taggings_count_before_type_cast.nil?
p taggings_count
p taggings_count_before_type_cast
      n = Tagging.count(:conditions => ['tag_id = ?', self.id])
      taggings_count = n
p n
p self.id
p taggings_count
p taggings_count_before_type_cast

    end
  end

>> Tag.find(:all, :conditions => 'taggings_count is NULL').each{|t|t.save}
1
nil
1
42
1
nil
=> [#<Tag:0x99c09c4 @taggings=[#<Tagging:0x99b9bd8
@attributes={"tag_id"=>"42", "id"=>"202", "taggable_type"=>"Post",
"taggable_id"=>"18"}>], @errors=#<ActiveRecord::Errors:0x99bf2cc
@base=#<Tag:0x99c09c4 ...>, @errors={}>,
@attributes={"name"=>"software", "taggings_count"=>nil, "id"=>"42"},
@new_record_before_save=nil>]

I tried a number of incarnations of this method, calling it validate,
and after_save with a save at the end.  nothing worked.

Happily, my string of ill-advised behavior ended and I updated to the
newest edge rails, 5982.  This fixed the original problem, and now new
tags are created with the correct taggings_count.  However, it seems to
me that this should work to fix the data, and yet it doesn't.  Why's
that?


Dag
This topic is locked and can not be replied to.