我正在研究Sinatra图像应用程序。我为我的用户,上传的图像和“Favs”定义了Datamapper模型。每个用户可以为每个图像提供最多一个“Fav”。
class User
include DataMapper::Resource
property :id, Serial
property :privilege_lvl, Integer, :default => 0
property :name, String, :unique => true
property :password_hash, BCryptHash
has n, :images
has n, :comments
has n, :favs
end
class Image
include DataMapper::Resource
property :id, Serial
mount_uploader :file, ImageUploader
belongs_to :user
property :posted_at, DateTime
has n, :comments
has n, :favs
end
class Fav
include DataMapper::Resource
property :id, Serial
belongs_to :image
belongs_to :user
end
有没有办法计算用户收到的收藏总数而不迭代用户的所有图片并总结每张图片的收视率?