我想重写UserItem
子类中的Item find_items
方法。 我应该将该方法添加为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