【发布时间】:2013-08-24 01:53:11
【问题描述】:
我有一个Location 模型,我使用 Rabl 以 json 格式呈现。我的 index.json.rabl 看起来是这样的:
object false
collection @locations
attributes :id, :name, :address, :rating
:rating 是根据Rating 模型中的记录计算得出的整数(Locationhas_manyRating、Ratingbelongs_toLocation)。
但现在我还想在 Rabl 文件中检索用于计算该值的 Rating 模型的行数。
我试过了:
child :ratings do
attributes :count
end
和
node(:ratings_count) { |m| @ratings.count }
但显然它不起作用......有人可以帮我吗?
谢谢!
【问题讨论】:
-
模型评分中是否有属性计数?我确定 child(:YOUR_HAS_MANY_ASSOC){attributes :YOUR_ATTRIBUTE} 工作正常。
-
您的意思是我需要在 Rating 中手动添加计数属性吗?我不能以某种方式在 Location 的收视率集合上使用 .count 吗?谢谢!
-
你的意思是要在 rabl 中使用 rating.count 吗?
标签: ruby-on-rails json rabl