2
这必须是一个容易的,但我卡... 所以我使用Rails#3与Mongoid,并希望动态生成查询,将取决于传递的参数和然后执行查找()。 喜欢的东西Mongoid动态查询
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
ok了,非常感谢你 – xaxa 2013-02-16 22:53:18
嗨 我不喜欢这样 查询= '' PARAMS [:事件]。每做|键,值| query + =“{#{key}:#{value.to_s}},”if value.present? 结束 events = Event.or(查询) 但是在MONGOID中不工作...你能帮忙吗 – 2014-06-26 07:11:15