开发者

Rails Model Inheritance?

开发者 https://www.devze.com 2023-03-14 06:06 出处:网络
Lets say I have a User model that has a field called subscriber that is a boolean and I want a subclass called Subscribe开发者_如何学Cr that is only Users with that field set to true. How can I do thi

Lets say I have a User model that has a field called subscriber that is a boolean and I want a subclass called Subscribe开发者_如何学Cr that is only Users with that field set to true. 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.subscribers
# => [#<User...>, #<User...>] # List of all subscribers
0

精彩评论

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