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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:54:21+00:00 2026-06-01T04:54:21+00:00

I have three models: Post , Comment , User and Vote . I’m using

  • 0

I have three models: Post, Comment, User and Vote.
I’m using polymorphic associations to make posts and comments votable
(users can vote +1 and -1).

Each time the user votes a post or a comment, its id is stored in the user_id foreign key of the vote (and the id of the post and comment should be stored in the votable_id and votable_type foreign keys as well).

post.rb:

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

  belongs_to :user

  has_many :comments, :dependent => :destroy
  has_many :votes, :as => :votable, :dependent => :destroy 
end

comment.rb:

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

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

  has_many :votes, :as => :votable, :dependent => :destroy 
end

user.rb (omitted the Devise part):

class User < ActiveRecord::Base        
  has_many :posts, :dependent => :destroy
  has_many :comments, :dependent => :destroy
  has_many :votes    
end

vote.rb:

class Vote < ActiveRecord::Base
  belongs_to :votable, :polymorphic => true
  belongs_to :user

  before_create :update_total

  protected

  // Update the value of total each time a vote is created
  def update_total
    self.total ||= 0
    self.total += self.polarity
  end
end

schema.rb (only included relevant parts, omitted stuff like created_at):

  create_table "comments", :force => true do |t|
    t.text     "content"
    t.integer  "post_id"
    t.integer  "user_id"
  end

  add_index "comments", ["post_id", "user_id"], :name => "index_comments_on_micropost_id_and_user_id"

  create_table "posts", :force => true do |t|
    t.string   "content"
    t.integer  "user_id"
    t.string   "title"
    t.integer  "comments_count", :default => 0, :null => false
  end

  create_table "users", :force => true do |t|
    t.string   "email",                                 :default => "", :null => false
    t.string   "username"
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

  create_table "votes", :force => true do |t|
    t.integer  "votable_id"
    t.string   "votable_type"
    t.integer  "user_id"
    t.integer  "polarity" // (+1 or -1)
    t.integer  "total"
  end

I have two questions:

  1. I’m not sure is the total should be a column in the posts or votes table.
  2. I know how to create a vote in the terminal (e.g. Vote.create(polarity => 1). But besides of that, I would like to know how to actually make the Devise current_user vote a post or a comment (in the terminal).

I would appreciate any help in the problems above.

  • 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-06-01T04:54:22+00:00Added an answer on June 1, 2026 at 4:54 am

    1) total column should be in the posts table.

    2) There’s no session in the console, you’re not executing any controller or routing code, there’s no current_user. Instead, just get any user and make him vote.

    u = User.first
    p = Post.first
    Vote.create(votable: p, user: u)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now, I have three models Post, Comment and User (using Devise ) associated
I have three models: Post ( has_many votes as votable ), Comment ( has_many
There are two models Post and Comment. I should get all posts, which have
I have a fairly standard blog app with the usual post and comments controller/models
I have 3 model, User, Post, Comment with definition like below class Post <
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
I have three models: class A < ActiveRecord::Base has_many :bs end class B <
I've encountered some problems with MongoID. I have three models: require 'mongoid' class Configuration

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.