2016-04-27 50 views
0

在我的Rails应用程序中,我试图为一个模型构建API,但我无法执行少量方法。有没有知道哪里错了。请提出建议。Rails 4 + Mongoid + API调用 - 无法定义几个条件

模式 - 用户

class User 
    include Mongoid::Document 
    include Mongoid::Timestamps 

    field :name, type: String 
    field :display_name, type: String 
    field :user_type, type: String 
    field :city_id, type: String 
    field :country_id, type: String 
    field :tags, type: String  
    field :status, type: Mongoid::Boolean 
    field :rating, type: Array 
end 

API 1 全部使用一览 - User.All

但我不能够采取选择列名。在模型中试图定义范围,但仍然给出错误。即。

User.selected_column

scope :selected_column, -> { select("id, name") } 

API 2. 顶级用户的列表 - User.rating_five

模型试图界定范围,但仍然给出错误。即。

User.rating_five

scope :rating_five, -> { .where(:rating["average"] > 4.6) } 

回答

0

Mondoid DSL是从ActiveRecord的在某些情况下,一个有点不同。请在这里提供官方documentation

第一个问题:

scope :selected_column, -> { only(:id, :name) }

中序来选择特定的列。

对于第二个问题,请尝试

scope :rating_five, -> { gt('rating.average': 4.6) }

这里gt代表greater than

+0

让我检查一下,让你知道,如果我发现任何问题。谢谢 – Rubyist