How to get aggregate totalling with data from two Models in DataMapper?

101 Views Asked by At

I am new to DataMapper and Ruby, and have been struggling to work out a way to format the expression which will give me what I am after. Let me explain. I have two Models in my project:

Class Manufacturer
  include DataMapper::Resource

  property :id, Serial
  property :name, String
  ...<snip>...

  has n, :items
end


Class Item
  include DataMapper::Resource

  property :id, Serial
  property :name, String
  ...<snip>...

  belongs_to :manufacturers
end

What I am trying to get is a collection which will give me the list of all manufacturers, plus a count of all items that they have produced. e.g.:

"Acme Industries", 32
"Bart Enterprises", 12
"Coco Mondo", 0
"XYZ Corp.", 55

That is, the :name from the Manufacturer model, and the count(:id) from the Item model. I've got as far as:

Manufacturer.all.items.aggregate(:manufacturer_id, :all.count)

which gives me the :manufacturer_id property and the correct count of items. Close, but no banana.

How can I get the manufacturer name rather that the id property in this case?

1

There are 1 best solutions below

1
On BEST ANSWER

I tried to learn how to use DataMapper a month ago and I could find very limited support in stackoverflow and in the internet. It turns out that the project is not supported any more and the team behind is now developing another orm called ROM.

Since you are in the beginning I would recommend you to use another orm with better support(sequel,ActiveRecords etc)