Ruby模型仅显示key = value的数据

时间:2012-03-14 12:57:26

标签: ruby-on-rails ruby activerecord

我有一个rails项目并且有一个模型customerssuppliers。我有一个名为people的sqlite数据库表。在客户模型中,我只想显示来自人员表的数据,其中type=customer和供应商仅显示人员表中type=supplier

的数据

关键是我对两个模型使用相同的表。如何才能使客户模型显示type=customer

2 个答案:

答案 0 :(得分:3)

转到单表继承。 single table inheritance

你需要从customers继承supplierspeople

答案 1 :(得分:1)

default_scope怎么样?

在客户中

default_scope where(:type => 'customer')

供应商

default_scope where(:type => 'supplier')

或者,您可以使用Single table inheritance。找到它here