impressionist icon indicating copy to clipboard operation
impressionist copied to clipboard

1% of records are missing impressionable_id

Open nafmarcus opened this issue 10 years ago • 1 comments

About 1% of my records are missing an impressionable_id, although they have all the other fields. This means that I have no way of knowing what "page' the user was actually visiting, only the type of page.

I am using friendly_id and therefore only using specific calls to impressionist(@object) to log entries. This records that are missing id's seem random in that they include almost all controllers and impressionable types.

Any suggestions?

nafmarcus avatar Aug 04 '14 13:08 nafmarcus

@nafmarcus I had this problem because I was using slugs (and hence the params hash doesn't have :id). Impressionist depends on this ... without it, it would create impressions with the impressionable_id set.

See this line ... :impressionable_id => params[:id].

    # creates a statment hash that contains default values for creating an impression.
    def direct_create_statement(query_params={})
      query_params.reverse_merge!(
        :impressionable_type => controller_name.singularize.camelize,
        :impressionable_id=> params[:id]
        )
      associative_create_statement(query_params)
    end

This is how I fixed it ...

module Something
  class ExamplesController < ApplicationController
    # Callbacks
    before_action :set_example, only: [:show]

    # Log impressions
    impressionist actions: [:show]

    # GET /something/:slug
    def show
    end

    private

    # Use callbacks to share common setup or constraints between actions.
    def set_example
      @example = Example.find_by! slug: params[:slug]
      add_example_id_to_params
    end

    # We have to add the id to params because the impressionist needs it.
    def add_example_id_to_params
      params[:id] = @example.id
    end
  end
end

itskingori avatar Dec 26 '14 09:12 itskingori