我加入了 Rails 团队并维护代码。有些对象是由 Gem virtus 控制的,但我真的不明白,就像下面的代码在做什么。
我理解属性'latest_book'可以从书籍中收集最新书籍的结果,但为什么可以这样做?'books=(books)' 在做什么?为什么“超级书籍”在这里?
class GetBooks
include Virtus.model
include ActiveModel::Model
attribute :books, Array[Book]
attribute :latest_book, Book
def books=(books)
self.latest_book = books.sort_by { |book| book['createdate'] }.last
super books
end
end
你可以帮帮我吗?