这一定很简单,但我被困住了......所以我将 Rails#3 与 Mongoid 一起使用,并希望动态构建取决于传递参数的查询,然后执行 find()。就像是
def select_posts
query = :all # pseudo-code here
if (params.has_key?(:author))
query += where(:author => params[:author]) # this is pseudo-code again
end
if (params.has_key?(:post_date))
query += where(:create_date => params[:post_date]) # stay with me
end
@post_bodies = []
Post.find(query).each do |post| # last one
@post_bodies << post.body
end
respond_to do |format|
format.html
format.json { render :json => @post_bodies }
end
end