【问题标题】:Rails Mongoid show all documents using "where"Rails Mongoid 使用“where”显示所有文档
【发布时间】:2015-05-07 09:56:35
【问题描述】:

我尝试将值作为参数传递给数组:

控制器:

@users = User.all.page(params[:page]).per(params[:per_page]).order(sort_by => sort_order).where(type: {'$all': ["#{params[:type]}"] })

现在,是否可以使用以下方法从集合中找到所有文档:

.where(type: {'$all': ['something_to_find_all_from type:'] })

?

【问题讨论】:

    标签: ruby-on-rails find mongoid


    【解决方案1】:

    假设 params[:type] 是一个数组,您可以使用 where 搜索所有用户,然后使用 order 对这些结果进行排序。你可以找到一个类似的例子here

    @users = User.where(:type.in => params[:type]).order(sort_by => sort_order).page(params[:page]).per(params[:per_page])
    

    如果您正在寻找翻译后的 mongoid 查询,您可以查看 this 链接

    然后您的查询可能会显示类似 User.where(:type.all => params[:type])

    【讨论】:

    • 不,我有 ActionView:Template::Error (操作:#<:protocol::command .where>
    • @Mike 你能详细说明你的问题吗?并发布模型代码和“params [:type]”的值?我还在上面添加了一个链接,以帮助您制定 mongoid 查询。
    猜你喜欢
    • 2011-07-29
    • 2011-07-23
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多