优艾设计网

Rails模型继承的时候,可否选择按条件继承?

现在有一模型: fruits ,
在其数据表中,有一字段 category, 用来区分不同 fruits ,

我们可否可以新建一个模型: apples ,
让其继承 category = 4fruits ?

也就意味着:

Apple.all 等于 Fruit.where(:category => 4)
Apple.new 时, Fruit.category 的值会默认为 4

在强大的Ruby面前,这应该如何实现?


我自己补充答案吧:

class Apple < Fruit defaul优艾设计网_设计百科t_scope { where(category: 4) } end


0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜