次の方法でUserオブジェクトをjsonに変換しています。
user.to_json :methods => :new_cookies
new_cookiesメソッドは次のとおりです。
cookies.all :include => :fortune, :conditions => {:opened => false}
これにより、ユーザーのjsonオブジェクト内にCookieが埋め込まれますが、幸運もCookieオブジェクト内に埋め込まれるようにしたいと思います。私は中を通り過ぎまし:include => :fortune
たが、それはうまくいきません。
これは可能ですか?
モデル:
class User < ActiveRecord::Base
has_many :cookies
has_many :fortunes, :through => :cookies
def new_cookies
cookies.all :include => :fortune, :conditions => {:opened => false}
end
end
class Cookie < ActiveRecord::Base
belongs_to :user
belongs_to :fortune
end
class Fortune < ActiveRecord::Base
serialize :rstatuses
serialize :genders
has_many :cookies
has_many :users, :through => :cookies
end