views:

73

answers:

1

Consider a class:

class Link < ActiveRecord::Base

  has_many :link_votes, :as => :vote_subject, :class_name => 'Vote'
  has_many :spam_votes, :as => :vote_subject, :class_name => 'Vote'

end

The problem is, when I'm adding a new vote with @link.link_votes << Vote.new the vote_subject_type is 'Link', while I wish it could be 'link_votes' or something like that. Is this an AR limitation or is there a way to workaround this thing?

I've actually found one related answer, but I'm not quite sure about what it says: http://stackoverflow.com/questions/1168047/polymorphic-association-with-multiple-associations-on-the-same-model/1764117#1764117

A: 

Sounds like you want to use Single Table Inheritance - this will allow you to have two different types of Votes. This will add a 'type' column to the votes table that you will then access as a LinkVote or SpamVote

class SpamVote << Vote
  ...
end

Along those lines.

class Link < ActiveRecord::Base

  has_many :link_votes, :as => :vote_subject
  has_many :spam_votes, :as => :vote_subject

end

In the votes table you'd see columns like:

id, type, vote_subject_type, vote_subject_id, etc.

Do more research on STI and I bet you'll find your answer.

Swards