Skip to content

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

@GraemeHarrison

Description

@GraemeHarrison

I keep getting this error:

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

For my setup, I added is_impressionable to my Posts model, and in the PostsController I am testing with a simple method like so:

def mark_as_viewed
    impressionist(@post)
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, as suggested in this post.

Here's a stack trace of the issue:

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", "testing@example.com"], ["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", "testing@example.com"], ["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", "testing@example.com"], ["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'

I have made sure that params[:id] is correct, @post is set correctly, and that current_user is set correctly, so I'm not sure what's causing this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions