I am working on a Rails 5 project and have a model called Upvote that is polymorphic and belongs to either an Event or Suggestion. I am trying to specifically grab upvotes that belong to one category or the other as well as the user id of whoever created the event/suggestion, but am struggling because of the polymorphism. There was a similar post on this a few years ago, but when I tried implementing the technique it didn't work. Specifically, I added this code to my Upvote model:
belongs_to :suggestion, -> { where(upvotes: {idea_type: 'Suggestion'}) }, foreign_key: "idea_id"
but when I try something like
Upvote.includes(:suggestion).last
in the console I get this error:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "upvotes"
LINE 1: SELECT "suggestions".* FROM "suggestions" WHERE "upvotes"."i...
Can anyone please help? Thanks :)