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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:50:08+00:00 2026-06-15T07:50:08+00:00

I have three models, Poem and Song and User . Users can vote on

  • 0

I have three models, Poem and Song and User.
Users can vote on any number of poems and songs.

One solution would be to make two association models PoemVote and SongVote

class PoemVote
  attr_accessible :poem_id, :user_id
  belongs_to :poem
  belongs_to :user
end

class SongVote
  attr_accessible :song_id, :user_id
  belongs_to :song
  belongs_to :user
end

I can call some_poem_vote.poem and some_song_vote.song

However, PoemVote and SongVote are essentially the same. How do I use single table inheritance to extend the two from one parent Vote class?

I’m thinking something along these lines:

class Vote
  attr_accessible :resource_id, :user_id
end

class PoemVote < Vote
  ...not sure what goes here...
end

class SongVote < Vote
  ...not sure what goes here...
end

How do I make it work so that I can still call some_poem_vote.poem but underneath have PoemVotes and SongVotes share one database table? Or is there a better solution for my problem?

  • 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-15T07:50:09+00:00Added an answer on June 15, 2026 at 7:50 am

    in rails, STI is simple : you just create a type string column on your votes table, and rails takes care of the rest. To create the right associations, you can do something like :

    class Vote
      attr_accessible :user, :votable
      belongs_to :user
      belongs_to :votable, polymorphic: true
    end
    

    …which would require to add a votable_id and a votable_type column on your votes table. Be sure to add

    has_many :votes, as: :votable, class_name: 'PoemVote' # or 'SongVote'
    

    on your associated model. However, problem with this approach is that you have to be vigilant and not use Vote directly to create votes, or you will have votes of the wrong type associated. To enforce this, there is a possible hack :

    class Vote
      attr_accessible :resource_id, :user_id
    
      def self.inherited( subclass )
        super( subclass )
        subclass.send :belongs_to, :votable,
                      class:  "#{subclass.name.gsub('Vote','')}"
      end
    end
    

    … but i know for sure (i struggled with te same problem) that it opens the door for code horror, as you have to resolve a lot of problems caused by inheritance (scopes behave weirdly, some libs don’t manage STI well, etc.).

    The question is: do you really need STI ? if your votes behave the same, don’t bother using STI, just use a polymorphic belongs_to, you’ll save yourself a lot of headaches.

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

Sidebar

Related Questions

I have three models: Post , Comment , User and Vote . I'm using
I have three models, one for Projects, other for Users and other to the
I have three models, Contender, Week and Vote, each contender can have votes based
I have three models User , Supplier , Company . There is One to
I have three models User , Post , Vote I need to doesn't allow
Possible Duplicate: CakePHP Subscribe Users Model I have three models User Post Friendship What
I have three models: class User include Mongoid::Document field :name, :type => String has_many
I have three models: User Image Group . User belongs to an image; Group
I have three models that are coming together to create one view model and
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>

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.