0

在 DataMapper 文档中给出Tag/Photo示例:

 class Photo
   include DataMapper::Resource

   property :id, Serial

   has n, :taggings
   has n, :tags, :through => :taggings
 end

 class Tag
   include DataMapper::Resource

   property :id, Serial

   has n, :taggings
   has n, :photos, :through => :taggings
 end

 class Tagging
   include DataMapper::Resource

   belongs_to :tag,   :key => true
   belongs_to :photo, :key => true
 end

我想选择所有没有照片的标签我知道我可以做

Tag.select { |tag| tag.photos.size < 1}

但我想要更多的 Datamapper 语法。就像是

Tag.all :photos.count.lt => 1  #not works

有没有办法做到这一点?有谁知道用于高级查询的不错的 Datamapper 文档?该站点上的文档非常好,但太基础了。

太极拳

4

2 回答 2

0

Tag.all(:photos => nil)将在 2 个 SQL 查询中得到你想要的。如果您更喜欢一个复杂的查询,则必须编写自定义 SQL 并使用repository(:default).adapter.select('SELECT ...')

于 2012-07-06T05:02:43.907 回答
0

尝试这个:

Tag.photos.all(:count.lt => 1)
于 2012-07-06T08:37:49.497 回答