如何返回子对象?
我的想法是一个简单的问题。这是我的代码:
class Fruit < ActiveRecord::Base
end
class Apple < Fruit
end
class Kiwi < Fruit
end
假设我正确设置了所有 STI,并且表中有多种类型的 Apple 和 Kiwi 记录。从这里...
fruits = Fruit.find(:all)
...我如何从水果数组中返回仅包含苹果的数组?
I have -- what I think -- is a simple question. Here's my code:
class Fruit < ActiveRecord::Base
end
class Apple < Fruit
end
class Kiwi < Fruit
end
Assume that I have all the STI setup correctly, and there are multiple types of Apple and Kiwi records in the table. From here...
fruits = Fruit.find(:all)
...how do I return an array of just Apples from the fruits array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它们只是普通对象,您也会这样做:
STI 使用
type
字段来跟踪子模型,因此您也可以这样做如果您只想从数据库中获取苹果,就做
The same way you would do it if they were just normal objects:
STI uses the
type
field to keep track of the submodel, so you could also doIf you want to get only the apples from the database, just do