2012-08-23 124 views
0

以前我是成功呈現一個客戶資源,包括它的使用下面的代碼,嵌套的優惠資源:渲染JSON:範圍的子資源

class Discount < ActiveRecord::Base 
    belongs_to :customer 
end 

format.json { render json: Customer.find(params[:id]), :include => { :discounts => 
    {:include => {...} 
    }} 
}} 

我後來加了範圍,我的折扣,即

class Discount < ActiveRecord::Base 
    belongs_to :customer 

    scope :current, where('(begin_on <= ? OR begin_on IS NULL) AND (? <= end_on OR end_on IS NULL)', Date.today, Date.today) 
end 

我如何使上述json調用僅渲染目前的折扣而不是所有的Customer.discounts?

回答

0

好的,所以我想出了一個解決方案。我會對任何替代答案感興趣,但對於我的目的,這似乎工作:

format.json { render json: @customer, :methods => [:current_discounts]} 

class Customer < ActiveRecord::Base 
    has_many :discounts 

    def current_discounts 
    discounts.current 
    end 
end