ruby-on-railsmerit-gem

Why is `merit_actions` appearing in this query without being specified?


In a production Rails 4.2 app, I am seeing the following log error whenever a Events#show page is called.

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "actions" SELECT "merit_actions".* FROM "merit_actions" WHERE (actions.trackable_type = 'Comment' AND actions.trackable_id in (NULL))

I am having trouble working out why/how this query is being created, and debugging the problem.

This query should be building an activity feed, based loosely on this Railscast. The Event model has a polymorphic Actionable association, through which many Comments and Photos are associated.

In the Event model is the following method

def action_feed
  fetch_comments    = Action.where( 'actions.trackable_type = ? AND actions.trackable_id in (?)', "Comment", self.comments.map(&:id) )
  fetch_photos      = Action.where( 'actions.trackable_type = ? AND actions.trackable_id in (?)', "Photo", self.leadings.map(&:id) )

  merged_actions  = fetch_comments | fetch_photos
  sorted_actions  = merged_actions.sort_by{ |a| a[:created_at] }.reverse
  paginated_actions = Kaminari.paginate_array( sorted_actions )
end

As you can see, the Action model is referenced, but there is no reference to merit_actions that is causing the problems in the query.

I do have the Merit gem installed

class User
  has_merit   
end

but not on the Event model.

Why is this query referencing merit_actions, and how can I debug this? It is not making sense to me.


Solution

  • Your Action model clashes with Merit's, which is surprising because Merit's is namespaced.

    Try specifying the root namespace on your calls, like ::Action.

    If this can be fixed in merit I'll gladly release the bugfix.