Forum: Ruby on Rails help with counter_cache

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.
Debcfd8f3f533800e2711c704825f9c0?d=identicon&s=25 Scott Kulik (kuliksco)
on 2008-12-19 01:43
I have two models:

Items
-id
-name
-created_by (links to user id)
...

Users
-id
-name
-items_count
...

item.rb:
belongs_to :user, :class_name => 'User', :foreign_key => 'created_by',
:counter_cache => true

user.rb:
has_many :items, :foreign_key => 'created_by'

---------------------------------------------

is this all i need to have it increment the User.items_count field
everytime a new item is created?  I'm not getting any error messages but
it's not updating the field.

can someone shed some light?
Debcfd8f3f533800e2711c704825f9c0?d=identicon&s=25 Scott Kulik (kuliksco)
on 2008-12-19 01:48
bah...i figured it out.  i didn't have a default value of 0 set in the
field so it wasn't updating.  now it's working!

the only thing now is that i have a couple thousand users who have 0 for
the count values.  what do you think is the best way i can mass update
this field?
6df9db0ace4cfd315296c344dc41ab1d?d=identicon&s=25 Bryce Roney (bdude)
on 2008-12-19 01:56
(Received via mailing list)
You can run a migration like this

def self.up
   User.find(:all) do |u|
     u.update_attribute :items_count, s.items.count
   end
end


That will go through all your users and update the items_count field
for all of them.
~~~~~~~~~~~~~~~
Bryce Roney
[ twitter ][ blog ][ myspace ][ facebook ]
Debcfd8f3f533800e2711c704825f9c0?d=identicon&s=25 Scott Kulik (kuliksco)
on 2008-12-19 02:13
Bryce Roney wrote:
> You can run a migration like this
>
> def self.up
>    User.find(:all) do |u|
>      u.update_attribute :items_count, s.items.count
>    end
> end
>
>
> That will go through all your users and update the items_count field
> for all of them.
> ~~~~~~~~~~~~~~~
> Bryce Roney
> [ twitter ][ blog ][ myspace ][ facebook ]

thanks for the quick response!

i just ran it but it didn't seem to update anything.

just curious what is the 's' for in s.items.count?
6df9db0ace4cfd315296c344dc41ab1d?d=identicon&s=25 Bryce Roney (bdude)
on 2008-12-19 02:29
(Received via mailing list)
Oh sorry! I was copying the code out of a project I had.

change s.items.count to u.items.count


-- Bryce Roney
Debcfd8f3f533800e2711c704825f9c0?d=identicon&s=25 Scott Kulik (kuliksco)
on 2008-12-19 06:36
Bryce Roney wrote:
> Oh sorry! I was copying the code out of a project I had.
>
> change s.items.count to u.items.count
>
>
> -- Bryce Roney

np!

i just tried it with the new code and it says its successful again but
items_count is still showing 0.  i checked @user.items.count in one of
my views and that works fine...must be something small.  here is my
output of the migration:

skulik@kuliksco-ub:/u1/app/wldev$ rake db:migrate
(in /u1/app/wldev)
== 20081218000000 UpdateItemsCount: migrating
=================================
== 20081218000000 UpdateItemsCount: migrated (0.1271s)
========================

skulik@kuliksco-ub:/u1/app/wldev$ cat
db/migrate/20081218000000_update_items_count.rb
class UpdateItemsCount < ActiveRecord::Migration

def self.up
   User.find(:all) do |u|
     u.update_attribute :items_count, u.items.count
   end
end

end
Debcfd8f3f533800e2711c704825f9c0?d=identicon&s=25 Scott Kulik (kuliksco)
on 2008-12-19 08:01
Bryce Roney wrote:
> You can run a migration like this
>
> def self.up
>    User.find(:all) do |u|
>      u.update_attribute :items_count, s.items.count
>    end
> end
>
>
> That will go through all your users and update the items_count field
> for all of them.
> ~~~~~~~~~~~~~~~
> Bryce Roney
> [ twitter ][ blog ][ myspace ][ facebook ]

i think i found the main issue.  I think there is supposed to be an each
in there like:

 def self.up
    User.find(:all).each do |u|
      u.update_attribute :items_count, u.items.count
    end
 end

the weird thing is that now the migration is taking 8 seconds so it
looks promising but the data is still not updated!  if i change
u.items.count to a constant like 1 it works fine, but it's not give me
error messages and @user.items.count works in my views so i'm not sure
what's wrong!

ah well, i guess i'll take another look at it tomorrow morning.  thanks
for the help so far!  hopefully someone can see my silly mistake.
Fac81a9d95c81d817a2debb6f717f3ac?d=identicon&s=25 Sazima (Guest)
on 2008-12-19 15:29
(Received via mailing list)
Maybe s.items.count is zero?

Cheers, Sazima

On Dec 19, 5:01 am, Scott Kulik <rails-mailing-l...@andreas-s.net>
Debcfd8f3f533800e2711c704825f9c0?d=identicon&s=25 Scott Kulik (kuliksco)
on 2008-12-19 17:04
Sazima wrote:
> Maybe s.items.count is zero?
>
> Cheers, Sazima
>
> On Dec 19, 5:01�am, Scott Kulik <rails-mailing-l...@andreas-s.net>

hmm...it shouldn't be since @user.items.count in my view shows the count
correctly for each user.
Debcfd8f3f533800e2711c704825f9c0?d=identicon&s=25 Scott Kulik (kuliksco)
on 2008-12-20 01:50
Scott Kulik wrote:
> Sazima wrote:
>> Maybe s.items.count is zero?
>>
>> Cheers, Sazima
>>
>> On Dec 19, 5:01�am, Scott Kulik <rails-mailing-l...@andreas-s.net>
>
> hmm...it shouldn't be since @user.items.count in my view shows the count
> correctly for each user.

is there another i can do it like this but not using a migration?

i tried to create a rake task but that didn't work.  here is my rake
task:

skulik@kuliksco-ub:/u1/app/wldev/lib/tasks$ cat update_items_count.rake
task :updateItemsCount do
  User.find(:all) do |u|
    u.update_attribute :items_count, s.items.count
  end
end

skulik@kuliksco-ub:/u1/app/wldev/lib/tasks$ rake updateItemsCount
(in /u1/app/wldev)
rake aborted!
uninitialized constant User

(See full trace by running task with --trace)

thanks!
Debcfd8f3f533800e2711c704825f9c0?d=identicon&s=25 Scott Kulik (kuliksco)
on 2008-12-20 02:12
Scott Kulik wrote:
> Scott Kulik wrote:
>> Sazima wrote:
>>> Maybe s.items.count is zero?
>>>
>>> Cheers, Sazima
>>>
>>> On Dec 19, 5:01�am, Scott Kulik <rails-mailing-l...@andreas-s.net>
>>
>> hmm...it shouldn't be since @user.items.count in my view shows the count
>> correctly for each user.
>
> is there another i can do it like this but not using a migration?
>
> i tried to create a rake task but that didn't work.  here is my rake
> task:
>
> skulik@kuliksco-ub:/u1/app/wldev/lib/tasks$ cat update_items_count.rake
> task :updateItemsCount do
>   User.find(:all) do |u|
>     u.update_attribute :items_count, s.items.count
>   end
> end
>
> skulik@kuliksco-ub:/u1/app/wldev/lib/tasks$ rake updateItemsCount
> (in /u1/app/wldev)
> rake aborted!
> uninitialized constant User
>
> (See full trace by running task with --trace)
>
> thanks!

got it.  i had to do this:

task(:updateItemsCount => :environment) do
  User.find(:all).each do |u|
    u.update_attribute :items_count, u.items.count
  end
end

it's still not working but at least i can test easier!
Debcfd8f3f533800e2711c704825f9c0?d=identicon&s=25 Scott Kulik (kuliksco)
on 2008-12-20 18:55
Scott Kulik wrote:
> Scott Kulik wrote:
>> Scott Kulik wrote:
>>> Sazima wrote:
>>>> Maybe s.items.count is zero?
>>>>
>>>> Cheers, Sazima
>>>>
>>>> On Dec 19, 5:01�am, Scott Kulik <rails-mailing-l...@andreas-s.net>
>>>
>>> hmm...it shouldn't be since @user.items.count in my view shows the count
>>> correctly for each user.
>>
>> is there another i can do it like this but not using a migration?
>>
>> i tried to create a rake task but that didn't work.  here is my rake
>> task:
>>
>> skulik@kuliksco-ub:/u1/app/wldev/lib/tasks$ cat update_items_count.rake
>> task :updateItemsCount do
>>   User.find(:all) do |u|
>>     u.update_attribute :items_count, s.items.count
>>   end
>> end
>>
>> skulik@kuliksco-ub:/u1/app/wldev/lib/tasks$ rake updateItemsCount
>> (in /u1/app/wldev)
>> rake aborted!
>> uninitialized constant User
>>
>> (See full trace by running task with --trace)
>>
>> thanks!
>
> got it.  i had to do this:
>
> task(:updateItemsCount => :environment) do
>   User.find(:all).each do |u|
>     u.update_attribute :items_count, u.items.count
>   end
> end
>
> it's still not working but at least i can test easier!

finally got the item count updated!  4 hours on this to realize i needed
to remove

:counter_cache => true

since i think it was protecting the items_count field from being
updated.
This topic is locked and can not be replied to.