# HABTM? Questions, Answers and Votes

**URL:** <https://www.ruby-forum.com/t/habtm-questions-answers-and-votes/49001>\
**Category:** Rails\
**Created:** [November 28, 2005, 4:05pm UTC](https://www.ruby-forum.com/t/habtm-questions-answers-and-votes/49001 "2005-11-28T16:05:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [November 28, 2005, 4:05pm UTC](https://www.ruby-forum.com/t/habtm-questions-answers-and-votes/49001/1 "2005-11-28T16:05:38Z")

</div>

Hi there, I’m new to rails and loving it, but I’ve hit a problem that  
I’m hoping to find an elegant solution for. I’ve been trawling thru  
this list and the wiki, but can’t quite find the last piece to the  
problem I’m trying to solve.

I’m trying to setup a simple Polling app, you know, posing questions and  
voting on answers. So far i have something (this is trimmed down) like:

class Question \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :answers  
has\_and\_belongs\_to\_many :votes  
belongs\_to :person  
end

class Answer \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :votes, :join\_table =\> ‘questions\_votes’  
end

class Vote \< ActiveRecord::Base  
belongs\_to :person  
has\_and\_belongs\_to\_many :answers, :join\_table =\> ‘questions\_votes’  
has\_and\_belongs\_to\_many :questions, :join\_table =\> ‘questions\_votes’  
end

With the following associated join table:

CREATE TABLE questions\_votes (  
question\_id INTEGER,  
answer\_id INTEGER,  
vote\_id INTEGER  
)

The above allows me to perform the following:

q = Question.find\_first  
a = q.answers.find\_first  
v = a.votes

But, if i try and go the other way, from a vote back to an answer, or  
from and answer back to a question, I am passed back an array. i.e.

v.questions or v.answers or a.questions are all valid.

But what i really want is the singular of the aboves, i know i could use  
find\_first, but that really shouldn’t be necessary. I understand why  
this is happening, due to the HABTM relationship.

I’ve tried using belongs\_to in the child classes, which works fine, i.e.

class Answer \< ActiveRecord::Base  
belongs\_to :question  
has\_and\_belongs\_to\_many :votes, :join\_table =\> ‘questions\_votes’  
end

But I’m then duplicating the foreign keys, which also doesn’t quite seem  
the most elegant approach either. given that the relationship already  
exists in the join table. Would i just be better off creating a new  
method on the child classes, something like:

class Answer \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :votes, :join\_table =\> ‘questions\_votes’

def find\_question  
self.questions.find\_first  
end  
end

It still seems like a kludge, and that the model isn’t quite  
representing that data in an accurate manner.

Can you suggest a better way of achieving the above? I’m probably  
missing something really obvious here. Maybe i just need to create a  
new  
Model for the relationships?

-vince

–  
keys: [http://codex.net/gpg.asc](http://codex.net/gpg.asc)

This country, with its institutions, belongs to the people who inhabit  
it.  
Whenever they shall grow weary of the existing government,  
they can exercise their constitutional right of amending it,  
or exercise their revolutionary right to overthrow it.  
– Abraham Lincoln

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [November 30, 2005, 2:32pm UTC](https://www.ruby-forum.com/t/habtm-questions-answers-and-votes/49001/2 "2005-11-30T14:32:12Z")

</div>

Vincent AE Scott wrote:

> has\_and\_belongs\_to\_many :votes  
> has\_and\_belongs\_to\_many :questions, :join\_table =\> ‘questions\_votes’  
> end

I would have expected an answer to be for (belong to) a single question,  
and a vote to be for (belong to) a single answer - in other words, I  
would expect one-to-many relationships rather than many-to-many.

Your statements below about the behaviour you want support this.

regards

```
Justin
```
