【问题标题】:mongoid query, select more field return in jsonmongoid查询,在json中选择更多字段返回
【发布时间】:2013-08-04 04:10:25
【问题描述】:

我想从 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

【问题讨论】:

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


    【解决方案1】:

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

    那么,解决方案可能是用 only(:title) 替换它

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

    to_json 应该可以工作。

    【讨论】:

    • 没有。我的意思是我想要获得更多不包含在数据库中的列。现在可以了。只需添加 attr_writer :title。然后调用 __.to_json(:methods => :title)。谢谢你的回答:)
    猜你喜欢
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 2015-04-28
    • 1970-01-01
    相关资源
    最近更新 更多