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

  • Home
  • SEARCH
  • 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 7583897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:47:36+00:00 2026-05-30T18:47:36+00:00

I just created new columns in my database for my micropost table and these

  • 0

I just created new columns in my database for my micropost table and these columns were vote_count comment_count and I want to connect it to the Vote models vote_up count and the Comment models comment count. Since I just added these columns although there were votes and comments, how do I connect these other models to the micropost model to fill in the new columns. Any suggestions are much appreciated!

Micropost Model

class Micropost < ActiveRecord::Base
  attr_accessible :title, :content, :view_count
  acts_as_voteable
  belongs_to :school
  belongs_to :user
  has_many :comments
  has_many :views
  accepts_nested_attributes_for :comments
end
  • 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-30T18:47:38+00:00Added an answer on May 30, 2026 at 6:47 pm

    It looks like what you’re trying to do is use a counter_cache, which rails supports, but you’ve got the names of the columns wrong.

    You want to add a comments_count and a votes_count column to your database instead of the ones that you have.

    Then you can hook it up to your models as follows:

    class Micropost< ActiveRecord::Base
      attr_accessible :title, :content, :view_count
      acts_as_voteable
      belongs_to :school
      belongs_to :user
      has_many :comments, :counter_cache => true
      has_many :views
      accepts_nested_attributes_for :comments
    end
    

    The votes half of it is a bit more tricky since you’re using some extra code with your acts_as_votable module, but counter caches are the way that you want to go if I understand you correctly.

    Here is more info on them: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

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

Sidebar

Related Questions

I just created a new table and filled it with data. When I run
I've just created a new view based application, and now I want to set
I just created a new rails app in Rails 3.1.1, and my application layout
I just created a new project using the visual studio cordova starter template. However
I've just created a new Windows XP VM on my Mac using VMware Fusion.
I have just created a new project in VS2010 using the Visual c# >
so I just created a new app that is references an older app of
Im using virtualenv on Fedora 16 and Ive just created a new virtualenv so
I could really use some help here. I just created a new bare repo
I'm new to silverlight and wpf programming I've just created a simple storyboard in

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.