0

以前は、以下のコードを使用して、ネストされた割引リソースを含む顧客リソースを正常にレンダリングしていました。

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ではなく、これらの現在の割引のみをレンダリングするにはどうすればよいですか?

4

1 に答える 1

0

さて、私は解決策を考え出しました。私は別の答えに興味がありますが、私の目的ではこれはうまくいくようです:

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

class Customer < ActiveRecord::Base
  has_many :discounts

  def current_discounts
    discounts.current
  end
end
于 2012-08-23T03:15:16.363 に答える