Datamapper:count favs

时间:2014-01-05 14:08:10

标签: ruby sinatra ruby-datamapper

我正在研究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

有没有办法计算用户收到的收藏总数而不迭代用户的所有图片并总结每张图片的收视率?

1 个答案:

答案 0 :(得分:0)

听起来你需要一个计数器缓存:

这可以简单到向用户添加:favs_cache并在添加优惠时递增它。

您可能还想查看此gem:dm-counter-cache