How can I fix this impressionist gem error?

161 Views Asked by At

I have a rails API that I use to create and serve up Posts to mobile clients. I am trying to use the Impressionist gem to track unique views of the Posts based on user_id. It seems simple enough to use, but I keep getting this error:

NoMethodError (undefined method `id' for {"init"=>true}:Hash):

Here's my setup: I added a views_count column to my Posts table, and in the model I have this:

is_impressionable :counter_cache => true, :column_name => :views_count, :unique => :user_id

Then in my PostsController I am trying to create the impression (ie view) in the show method, like so:

def show
    ...set @post...
    impressionist(@post)
    ...return @post json...
end

I can think of a few places where things may have gone wrong to cause the error. The first is that I changed all the id types in the gem's migration to use UUIDs. The result is this:

class CreateImpressionsTable < ActiveRecord::Migration[6.0]
  def self.up
    create_table :impressions, id: :uuid, :force => true do |t|
      t.string :impressionable_type
      t.uuid :impressionable_id
      t.uuid :user_id
      t.string :controller_name
      t.string :action_name
      t.string :view_name
      t.string :request_hash
      t.string :ip_address
      t.string :session_hash
      t.text :message
      t.text :referrer
      t.text :params
      t.timestamps
    end
    add_index :impressions, [:impressionable_type, :message, :impressionable_id], :name => "impressionable_type_message_index", :unique => false, :length => {:message => 255 }
    add_index :impressions, [:impressionable_type, :impressionable_id, :request_hash], :name => "poly_request_index", :unique => false
    add_index :impressions, [:impressionable_type, :impressionable_id, :ip_address], :name => "poly_ip_index", :unique => false
    add_index :impressions, [:impressionable_type, :impressionable_id, :session_hash], :name => "poly_session_index", :unique => false
    add_index :impressions, [:controller_name,:action_name,:request_hash], :name => "controlleraction_request_index", :unique => false
    add_index :impressions, [:controller_name,:action_name,:ip_address], :name => "controlleraction_ip_index", :unique => false
    add_index :impressions, [:controller_name,:action_name,:session_hash], :name => "controlleraction_session_index", :unique => false
    add_index :impressions, [:impressionable_type, :impressionable_id, :params], :name => "poly_params_request_index", :unique => false, :length => {:params => 255 }
    add_index :impressions, :user_id
  end

  def self.down
    drop_table :impressions
  end
end

The second is that when I initially installed the gem I was seeing a different error that said something like

undefined method `cookie_value' 

which I got around by installing a specific commit of the gem, as suggested in this post.

Any thoughts on what might be going on?

Update

Here is a stack trace for a slightly different method I made specifically for marking posts as viewed.

Method:

def mark_as_viewed
    impressionist(@post)
end

Stack trace:

app/controllers/api/posts_controller.rb:10:in `mark_as_viewed'
Started POST "/api/posts/3f278426-3c09-4b9d-b56d-9668ce559ebb/view" for ::1 at 2021-02-18 09:12:40 -0500
   (1.4ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by Api::PostsController#mark_as_viewed as JSON
  Parameters: {"id"=>"3f278426-3c09-4b9d-b56d-9668ce559ebb", "post"=>{}}
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."email" = $1 AND "users"."hidden_id" = $2 LIMIT $3  [["email", "[email protected]"], ["hidden_id", "YJcrbdHDZqeII2vq6TcvdM04zn42"], ["LIMIT", 1]]
  ↳ app/models/authentication_manager.rb:11:in `current_user'
  Post Load (1.0ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT $2  [["id", "3f278426-3c09-4b9d-b56d-9668ce559ebb"], ["LIMIT", 1]]
  ↳ app/controllers/api/posts_controller.rb:83:in `set_post'
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."email" = $1 AND "users"."hidden_id" = $2 LIMIT $3  [["email", "[email protected]"], ["hidden_id", "YJcrbdHDZqeII2vq6TcvdM04zn42"], ["LIMIT", 1]]
  ↳ app/models/authentication_manager.rb:11:in `current_user'
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."email" = $1 AND "users"."hidden_id" = $2 LIMIT $3  [["email", "[email protected]"], ["hidden_id", "YJcrbdHDZqeII2vq6TcvdM04zn42"], ["LIMIT", 1]]
  ↳ app/models/authentication_manager.rb:11:in `current_user'
Completed 500 Internal Server Error in 78ms (ActiveRecord: 19.9ms | Allocations: 28661)

NoMethodError (undefined method `id' for {"init"=>true}:Hash):

app/controllers/api/posts_controller.rb:10:in `mark_as_viewed'
0

There are 0 best solutions below