2

I'm using the PublicActivity gem: https://github.com/pokonski/public_activity

All of the models I use PublicActivity to track use the column edition_id. And I'm wondering how I can scope by that column since it's polymorphic relation.

Eg PublicActivity looks like so:

PublicActivity::Activity.limit(50)

+----+--------------+----------------+----------+------------+--------------------+------------+--------------+----------------+---------------------------+---------------------------+
| id | trackable_id | trackable_type | owner_id | owner_type | key                | parameters | recipient_id | recipient_type | created_at                | updated_at                |
+----+--------------+----------------+----------+------------+--------------------+------------+--------------+----------------+---------------------------+---------------------------+
| 1  | 42           | QuizMaster     | 6        | User       | quiz_master.update | {}         |              |                | 2013-04-12 17:33:14 +0100 | 2013-04-12 17:33:14 +0100 |
| 2  | 25           | Place          | 6        | User       | place.update       | {}         |              |                | 2013-04-12 17:42:42 +0100 | 2013-04-12 17:42:42 +0100 |
| 3  | 25           | Event          | 6        | User       | event.update       | {}         |              |                | 2013-04-12 17:45:08 +0100 | 2013-04-12 17:45:08 +0100 |
| 4  | 20           | QuizMaster     | 6        | User       | quiz_master.update | {}         |              |                | 2013-04-12 17:49:09 +0100 | 2013-04-12 17:49:09 +0100 |
| 5  | 20           | QuizMaster     | 6        | User       | quiz_master.update | {}         |              |                | 2013-04-12 17:50:51 +0100 | 2013-04-12 17:50:51 +0100 |
+----+--------------+----------------+----------+------------+--------------------+------------+--------------+----------------+---------------------------+---------------------------+

Where edition_id is on the polymorphic trackable relation.

What I would like to do it something like:

PublicActivity::Activity.limit(50).includes(:trackable)# where trackable edition = 1

I'm not sure how or even if it's possible to join or include or preload a polymorphic model across multiple "trackable" types.

ere
  • 1,739
  • 3
  • 19
  • 41
  • 1
    Can you explain a little more what you are trying to achieve? Eventually post an example of what you have at the beginning and what you want at the end. Also, I don't see `edition_id` column in the dumped data – MrYoshiji Jul 18 '14 at 14:06
  • Updated to try and be more specific thanks... – ere Jul 19 '14 at 12:13
  • Can you post the SQL query triggered when you do `PublicActivity::Activity.limit(10).joins(:trackable)` please? – MrYoshiji Jul 21 '14 at 14:16
  • `Cannot eagerly load the polymorphic association :trackable` I can use `includes` or `preload` but joins doesn't work, I assume since this is polymorphic and would need to join 4-5 difference models which it uses. – ere Jul 22 '14 at 14:21

1 Answers1

2

I don't think this is possible directly with SQL because of the polymorphism, but you could do something with ruby pretty easily.

PublicActivity::Activity.limit(50).select {|c| c.trackable.edition_id = 1 }

It won't be as efficient as a SQL query but using Rails preload it'll probably be good enough depending on the number of records you're fetching.

PublicActivity::Activity.limit(50).preload(:trackable).select {|c| c.trackable.edition_id = 1 }
holden
  • 13,471
  • 22
  • 98
  • 160