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 7189269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:15:58+00:00 2026-05-28T19:15:58+00:00

Right now, I have three models Post, Comment and User (using Devise ) associated

  • 0

Right now, I have three models Post, Comment and User (using Devise) associated as follows:

post.rb:

class Post < ActiveRecord::Base
  attr_accessible :title, :content, :total_votes

  validates :title,   :presence => true,
                      :length   => { :maximum => 30 },
                      :uniqueness => true
  validates :content, :presence => true,
                      :uniqueness => true

  belongs_to :user
  has_many :comments, :dependent => :destroy
end

comment.rb:

class Comment < ActiveRecord::Base
  attr_accessible :content, :user_id

  belongs_to :post, :counter_cache => true
  belongs_to :user
end

user.rb:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable
  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username

  validates_presence_of :username
  has_many :posts, :dependent => :destroy
  has_many :comments, :dependent => :destroy

  def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
    data = access_token.extra.raw_info
    if user = User.where(:email => data.email).first
      user
    else # Create a user with a stub password. 
      User.create!(:email => data.email, :password => Devise.friendly_token[0,20]) 
    end
  end
end

I want to add a fourth model called Vote with the following conditions:

  1. Both posts and comments can be voted (up and down) and show the total/sum.
  2. Each post will have many votes (up and down) and show the total/sum.
  3. Each comment will have many votes
  4. The ID of the user should be stored each time he or she votes so I can restrict one vote per user and show the ID/name of the users who voted (not sure where to store it)

Now, I’m not sure if this is a good occasion to use polymorphic associations and/or counter cache.

What’s an efficient way of associating these Post, Comment, User and Voting models?
(If possible, I would like to see how the migration would look like)

  • 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-28T19:15:59+00:00Added an answer on May 28, 2026 at 7:15 pm

    This is a perfect textbook example of where a polymorphic association would be useful.

    Your votes table migration would look like this:

    create_table :votes do |t|
      t.references :votable, :polymorphic => true
      t.references :user
      t.integer :polarity
      t.integer :total
    end
    

    This would create a table with this schema:

    id INTEGER
    votable_id INTEGER
    votable_type VARCHAR
    user_id INTEGER
    polarity INTEGER
    total INTEGER
    

    Here, user_id would be the person who cast the vote, polarity would be either ‘1’ for an upvote or ‘-1’ for a downvote (this lets you just sum the polarities to get upvotes and downvotes to cancel), votable_type would contain what the vote is for (Post or Comment), votable_id would contain the id of the thing the vote is for, and total would keep a running total of the vote sum (for efficiency).

    Then your models would look like this:

    class Vote < ActiveRecord::Base
        belongs_to :votable, :polymorphic => true
        belongs_to :user
    
        before_create :update_total
    
        protected
    
        def update_total
            self.total ||= 0
            self.total += self.polarity
        end
    end
    
    class Post < ActiveRecord::Base
        has_many :votes, :as => :votable
    end
    
    class Comment < ActiveRecord::Base
        has_many :votes, :as => :votable
    end
    
    class User < ActiveRecord::Base
        has_many :votes
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 model, User, Post, Comment with definition like below class Post <
I have an extended UserProfile model in django: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True)
I have a rails app which currently has 3 models: Post , User and
I'm developing a Grails App. I have about 20 Controllers right now and there
Good day! I right now have a function the drags an element from a
Right now I have a database (about 2-3 GB) in PostgreSQL, which serves as
Right now I have an SSIS package that runs every morning and gives me
Right now I have the following in my .vimrc : au BufWritePost *.c,*.cpp,*.h !ctags
Right now I have a few new applications being developed against an Oracle Database,
Right now I have a JSP page that allows to sort some items, when

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.