RoR 嵌套 :include 以包含 to_xml/to_json 中的子资源

发布于 2024-10-07 18:20:48 字数 798 浏览 0 评论 0原文

我一开始就有一个奇怪的数据模型情况,所以也许我的整个方法是错误的。这就是我正在做的事情:

我有一个名为 Bird 的类和一个名为 Color 的简单类。从概念上讲,每只鸟与颜色都有两种对多关联,一种与雄性颜色相关,一种与雌性颜色相关。我处理这个问题的方法是使用一个名为 BirdColoration 的连接模型,它属于一只鸟和一种颜色,并且有一个额外的布尔字段来判断颜色是雄性还是雌性。因此,每只鸟实际上与 BirdColoration 都有 to_many 关系,并且与 Color :through BirdColoration 也有 to_many 关系。如果这听起来合理,那么请继续阅读。否则,停下来告诉我为什么错了!

我需要能够将鸟类表转储为 json。以前,当每只鸟只有一个与颜色的 to_many 关联时,我可以使用 :include 将每只鸟的颜色包含在 json 转储中。现在,我将 BirdColorations 包含在转储中,但我仍然需要获取颜色模型本身。我可以分别包含每只鸟的颜色和颜色,然后在解析时将它们匹配,但我宁愿直接包含每种颜色的颜色。 然而,像上面这样的东西

      format.json  { render :json => @birds.to_json(:include => [{:bird_colorations => :color}, :seasons, :habitats, :image_holders]) }

不起作用。我认为这应该是可能的。谁能指出我如何处理这个问题的正确方向?

现在,我将分别包含每只鸟的颜色和色彩,并在解析时将它们进行匹配。至少我知道这会起作用。

谢谢!

I have a weird data model situation to start with, so maybe my whole approach is wrong. Here's what I'm doing:

I have a class called Bird and a simple class called Color. Conceptually, each bird has two to_many associations to Color, one for male colors and one for female colors. The way I've handled this is to use a join model called BirdColoration that belongs to a bird and a color and has an additional boolean field to tell if the coloration is for male or female. So each bird actually has a to_many relationship to BirdColoration as well as a to_many to Color :through BirdColoration. If this sounds reasonable, then continue reading. Otherwise, stop and tell me why it's wrong!

I need to be able to dump the birds table as json. Previously, when each bird only had one to_many association to colors, I could just use :include to include each bird's colors in the json dump. Now, I'm including the BirdColorations in the dump, but I still need to get at the color models themselves. I could separately include each bird's colors and colorations and then match them up while parsing, but I would much rather just include each coloration's color directly. Something like

      format.json  { render :json => @birds.to_json(:include => [{:bird_colorations => :color}, :seasons, :habitats, :image_holders]) }

The above doesn't work, however. I think that this should be possible. Can anyone point me in the right direction for how to handle this?

For now, I'll just include each bird's color and colorations separately and match them up in parsing. At least I know that will work.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不弃不离 2024-10-14 18:20:48

我在此处找到了答案。 to_xml 和 to_json 中 :include 选项的语法与 ActiveRecord 的 find 方法的语法不同。要以这种方式包含嵌套资源,请传入哈希而不是数组。对我来说正确的方法调用如下:

      format.json  { render :json => @birds.to_json(:include => {:bird_colorations => {:include => :color}, :seasons => {}, :habitats => {}, :image_holders => {}}) }

与我的问题中的方法进行比较以查看差异。对于您不想包含子资源的资源,只需传递一个空哈希作为其符号化名称的值即可。

活到老,学到老!

I found the answer here. The syntax for the :include option in to_xml and to_json is different than that for ActiveRecord's find method. To include nested resources in this way, you pass in a hash instead of an array. The correct method call for me looks like:

      format.json  { render :json => @birds.to_json(:include => {:bird_colorations => {:include => :color}, :seasons => {}, :habitats => {}, :image_holders => {}}) }

Compare to the one in my question to see the difference. For resources for which you don't want to include sub-resources, just pass an empty hash as the value for it's symbolized name.

Live and learn!

彩扇题诗 2024-10-14 18:20:48

如果您有复杂的 JSON 结构,最好在模型中覆盖 serialized_hashas_json,而不是尝试通过 render :json 来完成这一切。

所以像

def serializable_hash(options = nil)
  options ||= {}
  bird = {:name => name, ...}
  bird[:seasons] = seasons.serilizable_hash
  bird[:colors] = ... # whatever complex logic you want to get to the colors
  bird
end

两个函数这样的东西只需要返回一个哈希值。

If you have a complex JSON structure, it's better to override serializable_hash or as_json in your model than to try to do it all through render :json.

So something like

def serializable_hash(options = nil)
  options ||= {}
  bird = {:name => name, ...}
  bird[:seasons] = seasons.serilizable_hash
  bird[:colors] = ... # whatever complex logic you want to get to the colors
  bird
end

Both functions just need to return a hash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文