At 7:47 PM -0300 10/12/08, Damian Janowski wrote:
>On Sat, Oct 11, 2008 at 1:25 AM, Stephen Bannasch
><[EMAIL PROTECTED]> wrote:
>>
>> Thanks Frederic and Duncan,
>>
>> FYI: here's the complete migration for the simple test case that adds
>> a counter_cache AND sets the value.
>
>Or you could use http://rails.lighthouseapp.com/projects/8994/tickets/228 :-)
Thanks for that pointer Damian. I didnm't know about the class method
Model.update_counters.
I've simplified my migration as follows:
class AddCounterCacheToLearners < ActiveRecord::Migration
def self.up
add_column :learners, :learner_sessions_count, :integer, :default => 0
Learner.reset_column_information
Learner.find(:all).each do |learner|
change_in_count = learner.learner_sessions.count -
learner.learner_sessions_count
Learner.update_counters(learner.id, :learner_sessions_count =>
change_in_count)
end
end
def self.down
remove_column :learners, :learner_sessions_count
end
end
While this is better than my previous solution it still seems a bit ugly.
I like your proposed patch but it's marked as "wontfix".
In my simplified migration above this section:
Learner.reset_column_information
Learner.find(:all).each do |learner|
change_in_count = learner.learner_sessions.count -
learner.learner_sessions_count
Learner.update_counters(learner.id, :learner_sessions_count =>
change_in_count)
end
is equivalent to the suggestions at the end of your patch discussion for a
class method something like this:
Learner.update_counter_cache
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---