0

Give the Tag/Photo exemple on DataMapper documentation:

 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

I want to select all tags that has no photos I know I can do

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

But I'd like more Datamapper syntax. Something like

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

Is there a way to do this? Does anyone knows a nice Datamapper documentation for advanced queries? Documentation at the site is very nice, but too basic.

Tkz

cpereira
  • 155
  • 9

2 Answers2

0

Tag.all(:photos => nil) will get what you want in 2 SQL queries. If you prefer one complex query, you have to write a custom SQL and execute it with repository(:default).adapter.select('SELECT ...')

ujifgc
  • 2,075
  • 1
  • 18
  • 20
0

Try this:

Tag.photos.all(:count.lt => 1)
solnic
  • 5,583
  • 2
  • 21
  • 19
  • This only worked for me if you have an attribute called count on photos.... But it is not the case.. – cpereira Jul 26 '12 at 20:07