I have created this module:
app/models/concerns/sluggable.rb
module Sluggable
extend ActiveSupport::Concern
included do
before_create :set_slug
end
def set_slug
if self.slug.nil?
puts 'adding slug'
self.slug = SecureRandom.urlsafe_base64(5)
end
end
end
and I include it in a model, like so:
app/models/plan.rb
class Plan < ApplicationRecord
extend FriendlyId
friendly_id :id, :use => :slugged
include Sluggable
end
but the before_create does not fire. The slug column is a not_null column so I get a database error.
ERROR: null value in column "slug" violates not-null constraint
If put the set_slug code directly in the model, it works. So what am I missing here about Concerns in Rails 5?
I wonder if its something related to using FriendlyId (which is why I added slugs in the first place!)