我通过以下方式将用户对象转换为 json:
user.to_json :methods => :new_cookies
new_cookies 方法是:
cookies.all :include => :fortune, :conditions => {:opened => false}
这将 cookie 嵌入到用户 json 对象中,但我希望幸运也嵌入到 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