We have objects that we want to represent in stacks (think of stacking items in an MMO). There will be duplicate rows.
Let’s say our owned_objects table looks like this.
user_id | object_id
1 | 27
1 | 27
3 | 46
3 | 46
5 | 59
I want the query to do
SELECT
user_id,
object_id,
count(*) AS count
FROM owned_objects
GROUP BY
user_id,
object_id;
And return either the 3 distinct OwnedObjects (or even just getting the distinct Objects would work too) and a count associated with it.
I know this is possible with SQLAlchemy, but can you do it with ActiveRecord?
How about …
… or similar?
You can then retrieve the counts by a dynamically created attribute on each object:
This assumes either a
has_and_belongs_to_many :objectsor ahas_many :objects, :through => :owned_objectson user.