mongoid查询,在json中选择更多字段返回

时间:2013-08-04 04:10:26

标签: ruby json ruby-on-rails-3 mongoid

我想从2台设备中获取2个url图像。我的模型结构:

比较:has_one:device1,has_one:device2

设备:has_one:image

图片:mount_uploader:image,ImageUploader

在查询中我想从查询中获取JSON数组比较

@compares = Compare.any_of({:device1_id.in => arr_device_id},{:device2_id.in => arr_device_id})

我尝试:(标题是attr_accessor)

Compare.any_of({:device1_id.in => arr_device_id},{:device2_id.in => arr_device_id}).only(:title => 'abc'). Result json is not contain field title


@compare.to_json(:include => [:device1,:device2]) => this include device1 and device2 but not include image of it.


@compare.to_json(:method => [:title => 'abc'])   . Result json is not contain field title

任何解决方案?谢谢你的帮助:D

1 个答案:

答案 0 :(得分:0)

您的意思是only(:title => "abc")只选择字段“title”吗?

然后,解决方案可能只用(:title)替换它

如果你的意思只是标题是“abc”,那么用only(:title => "abc")替换where(:title => "abc")

to_json应该可以工作。