charlotte-ruby / impressionist

Rails Plugin that tracks impressions and page views
MIT License
1.54k stars 313 forks source link

Mongoid::Errors::UnknownAttribute when trying to use a model with slugs #253

Closed fibrasek closed 7 years ago

fibrasek commented 7 years ago

Hello guys,

I've been running in this error when setting up the gem with mongoid:

message: Attempted to set a value for 'params' which is not allowed on the model Impression. summary: Without including Mongoid::Attributes::Dynamic in your model and the attribute does not already exist in the attributes hash, attempting to call Impression#params= for it is not allowed. This is also triggered by passing the attribute to any method that accepts an attributes hash, and is raised instead of getting a NoMethodError. resolution: You can include Mongoid::Attributes::Dynamic if you expect to be writing values for undefined fields often.

I tried to lookup in the gem code and in the documentation for something related, bug couldn't find anything.

Have anyone run into this?

Heres my (pretty basic) controller:

class Public::HomeController < PublicController
  def show
    @party = Party.find(params[:id])
    impressionist(@party)
  end
end

And my model (filtered unrelated stuff for brevity):

class Party
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Orderable
  include Mongoid::Slug
  include Mongoid::Paperclip

  # Modules
  include Party::CreationStatus
  include Party::Guests

  # Track views
  is_impressionable

  # Slugs
  slug :url

  # Fields
  field :url, type: String

  ...
end
fibrasek commented 7 years ago

A bit more digging and testing, I found out that the mongoid/impression.rb was missing the field params.

I'll send a PR with the fix.

rchrand commented 7 years ago

Issue have been fixed, closing.