What is a good template for querying embedded documents in a document? For example, in my User document there is an embedded Alerts document. If I want to find out if a given user has a warning with a name, I can do this in two ways, as far as I can tell - in memory a la
alert = current_user.alerts.select{|a| a.name == params[:name]}.first
or through the a la document interface (note that I'm not 100% sure that this is semantically valid, but you get the point):
User.where('alerts.name' => params[:name], :id => current_user.id).first
There MUST be a better way, something like
current_user.alerts.where(:name => params[:name])
may be? Or maybe I'm just not thinking about the problem correctly?
source share