[英]Rails 3 to_json include with condition/where clause
我正在使用以下内容来输出JSON记录列表:
@team.people.to_json(
:include => [:user, :statistics => {:include => :attribute}]).html_safe
但是,我只想包括设置了特定type_id的统计信息。 本质上是与用户和统计信息的左外部type_id
,其中统计信息中的type_id
等于某个数字。
我至少可以想到几个选择:
Person
模型中,重写to_json
(或者可能更好的是serializable_hash
),然后在此处进行条件设置。 {:include => :attribute}
来做{:methods => :foo}
并在foo
做条件式。 这是我覆盖serializable_hash
的示例(如果有帮助):
def serializable_hash(options={})
options = {
:methods => [
'client',
'services',
'products',
'has_payments',
]}.update(options)
super(options)
end
我可以想象在options =
之上,如果type_id
是您要查找的数字,则将methods数组设置为一件事,否则设置为其他事物。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.