ruby on rails - When using include(.) with conditions, how can I make sure I still get results from the first table, even if there are no matches on the included one? -
given 4 models:
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 i gradebook entries in gradebook, including associated gradebook_entry_score, 1 belongs particular user.
this how right now:
some_gradebook.gradebook_entries.includes(:gradebook_entry_scores).where(:gradebook_entry_scores => {:user_id => 50} ) and works. except if there no gradebook_entry_score user, no gradebook_entry returned.
some_gradebook.gradebook_entries.includes(:gradebook_entry_scores).where(:gradebook_entry_scores => {:user_id => 50} ) => [] i gradebook_entries, regardless of whether there gradebook_entry_score user or not.
so, how can make sure results first table, if there no results included table?
edit 1:
a user may not have gradebook_entry_score particular gradebook_entry.
i expect .where(:gradebook_entry_scores => {:user_id => 50} ) problem here.
what if start user in question, like:
user.find_by_id(50).gradebooks.gradebook_entries.includes(:gradebook_entry_scores)
Comments
Post a Comment