charlotte-ruby/impressionist

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

Closed this issue · 2 comments

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

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.

Issue have been fixed, closing.