この 4 つのモデルを考えると、次のようになります。
class Gradebook < < ActiveRecord::Base
has_many :gradebook_entries
end
class User < ActiveRecord::Base
has_many :gradebook_entry_scores
end
class GradebookEntry < ActiveRecord::Base
has_many :gradebook_entry_scores
end
class GradebookEntryScore < ActiveRecord::Base
belongs_to :gradebook_entry
belongs_to :user
end
関連する gradebook_entry_score を含む、成績表のすべての成績表エントリを取得したいと考えていますが、特定のユーザーに属するものだけを取得したいと考えています。
これが私が今やっている方法です:
some_gradebook.gradebook_entries.includes(:gradebook_entry_scores).where(:gradebook_entry_scores => {:user_id => 50} )
そして、それは機能します。ただし、そのユーザーの gradebook_entry_score がない場合、gradebook_entry は返されません。
some_gradebook.gradebook_entries.includes(:gradebook_entry_scores).where(:gradebook_entry_scores => {:user_id => 50} )
=> []
そのユーザーの gradebook_entry_score があるかどうかに関係なく、常にすべての gradebook_entries を取得したいと考えています。
では、含まれているテーブルからの結果がない場合でも、最初のテーブルから結果を取得するにはどうすればよいでしょうか?
編集1:
ユーザーは、特定の gradebook_entry の gradebook_entry_score を持っていない場合があります。