2016-04-27 28 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

相關問題