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