我正在开发一款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是否有一种方法可以计算用户接收到的总数,而不需要迭代用户的所有图像并总结每个图像的Favs?
发布于 2014-01-05 17:50:34
https://stackoverflow.com/questions/20934512
复制相似问题