find_items
サブクラスのItemのメソッドをオーバーライドしたいと思いますUserItem
。protected
そのメソッドをまたはとして追加する必要がありprivate
ますか?
保護されたメソッドはサブクラスで使用でき、プライベートはそれらが属するクラスでのみ使用できるようになりました。
class Item
def item_ids
@ids ||= REDIS.zrevrange(key, 0, 100).map(&:to_i)
end
def items
find_items
item_ids.collect {|id| records.detect {|x| x.id == id}}.compact.map(&:content)
end
protected
def items_scope
Item
end
def find_items
items_scope.find(item_ids)
end
end
class UserItem < Item
def initialize(user)
@user = user
end
# should I add it here?
protected
# or here?
def items_scope
Item.where(user_id: @user.id).not_anonymous
end
end
メソッドのオーバーライド:
def find_items
items_scope.where(id: item_ids)
end