0

I'm using the Public Activity gem to track user's comments. I would like to get fetch all unique user comments in a query. I tried doing the following:

PublicActivity::Activity.where(:trackable_type=>"Comment").where(:owner_id => user.id).all.select(:trackable_id).distinct

But I'm getting the error:

ArgumentError: wrong number of arguments(1 for 0)
    from (irb):14:in `select'
    from (irb):14
    from /Users/ttseng/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.15/lib/rails/commands/console.rb:47:in `start'
    from /Users/ttseng/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.15/lib/rails/commands/console.rb:8:in `start'
    from /Users/ttseng/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.15/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Does anyone know how to extract of a user's unique activities based on trackable_id?

For reference, this is what my query results look like before I attempt to fetch the unique records:

enter image description here

scientiffic
  • 9,045
  • 18
  • 76
  • 149

2 Answers2

2

I did following on Rails Console, see what i got

 2.0.0p353 :020 > PublicActivity::Activity.where(id:1).class
 => ActiveRecord::Relation::ActiveRecord_Relation_PublicActivity_Activity 
2.0.0p353 :021 > PublicActivity::Activity.all.class
 => ActiveRecord::Relation::ActiveRecord_Relation_PublicActivity_Activity 
2.0.0p353 :022 > PublicActivity::Activity.where(id:1).all.class
DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. `Post.where(published: true).load`). If you want to get an array of records from a relation, you can call #to_a (e.g. `Post.where(published: true).to_a`). (called from irb_binding at (irb):22)
W, [2014-09-06T01:47:09.341145 #8594]  WARN -- : DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. `Post.where(published: true).load`). If you want to get an array of records from a relation, you can call #to_a (e.g. `Post.where(published: true).to_a`). (called from irb_binding at (irb):22)
  PublicActivity::Activity Load (0.6ms)  SELECT "activities".* FROM "activities" WHERE "activities"."id" = 1
D, [2014-09-06T01:47:09.343063 #8594] DEBUG -- :   PublicActivity::Activity Load (0.6ms)  SELECT "activities".* FROM "activities" WHERE "activities"."id" = 1
 => Array 
2.0.0p353 :023 > 

As you can see,u are performing select operation on an array, that's why u are get such error ArgumentError: wrong number of arguments(1 for 0)

try this instead,

 PublicActivity::Activity.where(:trackable_type=>"Comment").where(:owner_id => user.id).select(:trackable_id).distinct
Rahul Singh
  • 3,417
  • 2
  • 25
  • 32
  • thanks for your response. when I run the query without "all", I get a different error: Hirb Error: missing attribute: viewed /app/config/initializers/public_activity.rb:34:in `init' – scientiffic Sep 05 '14 at 20:43
  • it turns out it was an issue with HIRB, which is a different problem. When I remove the custom initialization code, it works. thanks! – scientiffic Sep 05 '14 at 20:46
  • actually, this returns an array of trackable_ids rather than the actual public activity records. is it possible to return the records? – scientiffic Sep 05 '14 at 20:59
  • this is what "select" does, u can add more fields inside select. – Rahul Singh Sep 05 '14 at 21:02
  • if you add more to the select, how does it know which field to do the uniq on? – scientiffic Sep 05 '14 at 21:04
0

The answer was to run the following:

PublicActivity::Activity.where(:trackable_type=>"Comment").where(:owner_id => user.id).select("DISTINCT ON (trackable_id) *")

scientiffic
  • 9,045
  • 18
  • 76
  • 149