Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4071398
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:45:06+00:00 2026-05-20T16:45:06+00:00

My models look like this: class Movie < ActiveRecord::Base attr_accessible :title, :year, :rotten_id, :audience_score,

  • 0

My models look like this:

class Movie < ActiveRecord::Base
  attr_accessible :title, :year, :rotten_id, :audience_score,
    :critics_score, :runtime, :synopsis, :link, :image

  has_many :jobs, :dependent => :destroy
  has_many :actors, :through => :jobs
end

class Actor < ActiveRecord::Base
  attr_accessible :name
  has_many :movies, :through => :jobs
  has_many :jobs, :dependent => :destroy
end

class Job < ActiveRecord::Base
  attr_accessible :movie_id, :actor_id

  belongs_to :movie
  belongs_to :actor
end

When I’m displaying my index of Actors, I’d like to show the number of movies each actor has starred in. I can do this with @actor.movies.count, however this generates an SQL query for each actor. With, say, 30 actors, this will result in 30 extra queries in addition to the initial.

Is there any way to include the count of movies each actor has participated in, in the initial Actor.all call? And thereby getting things done with only one call. Extra bonus if this was sorted by said count.

Update:
All answers provided has been helpful, and though it turned into some dirt-slinging-contest at some point, it worked out well. I did a mish-mash of all your suggestions. I added a movies_counter column to my Actor model. In my Job model I added belongs_to :actor, :counter_cache => :movies_counter. This works brilliantly, and is automatically updated when i create or destroy a movie, without me adding any further code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:45:07+00:00Added an answer on May 20, 2026 at 4:45 pm

    As @Sam noticed, you should add new column to actors table movies_counter

    rails g migration add_movies_counter_to_actor movies_counter:integer
    

    Now you can edit your migration

    class AddMoviesCounterToActor < ActiveRecord::Migration
      def self.up
        add_column :actors, :movies_counter, :integer, :default => 0
    
        Actor.reset_column_information
        Actor.all.each do |a|
          a.update_attribute :movies_counter, a.movies.count
        end
      end
    
      def self.down
        remove_column :actors, :movies_counter
      end
    end
    

    And run it

    rake db:migrate
    

    Then you should add two callbacks: after_save and after_destroy

    class Movie < ActiveRecord::Base
      attr_accessible :title, :year, :rotten_id, :audience_score,
        :critics_score, :runtime, :synopsis, :link, :image
    
      has_many :jobs, :dependent => :destroy
      has_many :actors, :through => :jobs
    
      after_save :update_movie_counter
      after_destroy :update_movie_counter
    
      private
      def update_movie_counter
        self.actors.each do |actor|
          actor.update_attribute(:movie_count, actor.movies.count)
        end
      end
    end
    

    Then you can call some_actor.movies_counter

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
If my models look like this: (app/models/letter.rb) class Letter < ActiveRecord::Base def cyrilic_equivilent #
Let's say I have models that look like this: class Foo < ActiveRecord::Base has_many
The models I'm working with look like this: class ComplexAssertion < ActiveRecord::Base has_many :expression_groups
I've got a set of models that look like this: class Page(models.Model): title =
I have a model that looks like this: class Movie(models.Model): title = models.CharField(max_length=200) slug
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
I've got two models that look like this class Stage include DataMapper::Resource property :id,
I have a django model and model form that look like this: -models.py class
So I have a few Django models that look like this: class City(models.Model): name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.