[eluser]dulakian[/eluser]
I can't seem to make the through relationship work the way I understand it should. Here is my db layout:
Code:
Users
-id
Pages
-id
-user_id
Polls
-id
-page_id
Questions
-id
-poll_id
Polls_Answers
-id
-poll_id
-answer_id
Answers
-id
I've set my relationships like this:
User has many pages
Page has many polls
Poll has one Question
Polls habtm Answers
User has many polls through pages
My first question is do I have to reverse the through relationship? Meaning in the Poll model do I have to set a belongs_to relation to Users, or something similar? I'm a little unclear how I would reverse the through relation, if that's even necessary here. You didn't do a reverse on your example, but you were using the factory instead of the model as I am...
When I try to access the related polls from the user model, it expects polls to have a user_id field, is this normal? I thought it would relate through the user_id in Pages, not a field in Polls. If I have to add a user_id field to Polls, it seems to defeat the purpose of the through relationship imo, I could just set up a has_many from users to polls, and has_many from pages to polls. The habtm relation from polls to Questions and Answers works perfectly, I can pull an entire poll with a single query, which is exactly how I want it (using 2 join_related commands to put the Question and all Answers together). I'd like to get all polls, with Question and Answers, by user_id, and thought the through relationship would work for this. I think I'm missing something simple here but don't see it...
Thanks for such a great library, it really rocks not to have to code out all the relationships by hand.
Michael