views:

43

answers:

1

Given the relations

{
    proposals(id, ...),
    reviewers(id, ...),
    comments(id, user_id, proposal_id, ...),
    votes(id, user_id, proposal_id, ...)
}

how can I create an association from Vote to Comment? Each reviewer can vote once on a proposal "create unique index on votes(user_id, proposal_id)" and can comment many times. A reviewer can comment and not vote or vote and not comment so their is no dependency between votes and comments. From the vote model I wish to associate the many comments that match on (user_id, proposal_id). These are the comments that are relevant to a reviewers vote on a proposal.

The association

class Vote < ActiveRecord::Base
  belongs_to :reviewer
  belongs_to :proposal
  has_many :comments, :through => :proposal
end

will yield comments from all reviewers. Likewise

has_many :comments, :through => :reviewer

will yield comments from all proposals. I'd like the intersection of the above two sets of comments.

Is it possible to

has_many :comments, :through => [:reviewer, :proposal]

or

has_many :comments, :through => :reviewer, :scope => :proposal_id

Neither of these work. What is the best way to work around this - or do I just need to read more documentation?

+1  A: 

I don't think you'd have much luck with that. I'd try something more simple and just do a comments method to the Vote model, such as:

def comments
   Comment.find_all_by_proposal_id_and_user_id(self.proposal_id,self.user_id)
end

And just like that, you'd be all setup.

Yaraher