views:

18

answers:

0

Hi there,

I'm sorry the title is kinda vague :-)

I have a User model with associated Answers. Each of these answers is an answer to a Question:

class User
   has_many :answers
end

class Question
   has_many :answers
end

class Answer
   belongs_to :question
   belongs_to :user
end

The questions are the same for all users, but their answer to these defined questions differ (and can be empty / non-existing).

I can obviously just access User.first.answers, but this will - naturally - only give me the Q&As with answers.

I've tried adding a :finder_sql to my User->Answers-relation:

class User
   has_many :answers,
            :finder_sql => 'SELECT a.*, q.id AS question_id FROM questions q LEFT JOIN answers a ON a.question_id = q.id AND a.user_id = #{id}' 
end

..and this works as described, but I find it pretty ugly (and it yields some nasty SQL when calling, for instance, User.first.answers.all)

Is there a better way to do this?