**わかりやすくするために編集**
**編集の名前がドキュメントに変更されました**
特定のユーザーによる投票レコードを含むすべてのユーザードキュメントレコードを取得したいのですが、そのユーザーが一部のドキュメントレコードに投票されていない場合でも、投票なしですべてのドキュメントレコードを取得したいと思います。egzampleの場合:
+------------+--------------+------+
|document.par1|document.par2| vote |
+------------+--------------+------+
| 2 | z | y |
| 3 | w | NULL |
| 4 | x | NULL |
+------------+--------------+------+
Ruby on Railsでこれを試した場合:
I.最初の試み:
Document.
joins("LEFT JOIN `votes` ON `votes`.`v_id` = `document`.`id` AND `votes`.`giver_id` = ?", user_id).
where('document.creator_id = ?', user_id, .....).
select('votes.*', `document.param1')
このエラーが発生しました:
RuntimeError in UserDocumentsController#show
unknown class: Fixnum
II。2回目の試行:
Document.
joins("LEFT JOIN `votes` ON `votes`.`v_id` = `document`.`id`").
where('document.creator_id = ? AND `votes`.`giver_id` = ?', user_id, user_id, .....).
select('votes.*', `document.param1')
投票があるレコードのみを返します。
+-------------+-------------+------+
|document.par1|document.par2| vote |
+-------------+-------------+------+
| 2 | z | y |
+-------------+-------------+------+
したがって、2つのレコードが欠落しています。
**更新されました。このソリューションは機能します**
III。私の3回目の試み、助けてくれてありがとうMischa:
Document.
joins("LEFT JOIN `votes` ON `votes`.`v_id` = `document`.`id`").
where('document.creator_id = ? AND (`votes`.`giver_id` = ? OR `votes`.`giver_id` IS NULL)', user_id, user_id).
select('votes.*', `document.param1')
+-------------+-------------+------+
|document.par1|document.par2| vote |
+-------------+-------------+------+
| 2 | z | y |
| 3 | w | NULL |
| 4 | x | NULL |
+-------------+-------------+------+