[英]Rails Model Inheritance?
Lets say I have a User model that has a field called subscriber that is a boolean and I want a subclass called Subscriber that is only Users with that field set to true. 假设我有一个User模型,该模型具有一个名为Subscriber的字段,该字段是布尔值,并且我想要一个名为Subscriber的子类,该子类只有将该字段设置为true的Users。 How can I do this and am I approaching this the wrong way?
我该怎么做,我是否正以错误的方式走?
If that's the only difference, you might want to look at using scopes instead: 如果那是唯一的区别,那么您可能要考虑使用范围:
class User < ActiveRecord::Base
scope :subscribers, where(:subscriber => true)
end
Then you can access the subscribers as a method on the User
class: 然后,您可以作为
User
类上的方法访问订户:
User.subscribers
# => [#<User...>, #<User...>] # List of all subscribers
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.