I know there are some gems out there that give you slugs out of the box. However, I think doing something similar is a good way of learning.
Since I'm using slugs in several models, I decided to create a module and include it via ActiveSupport::Concern.
This is my module sluggable.rb:
module Sluggable
extend ActiveSupport::Concern
module ClassMethods
def slug(*atts_to_slug)
after_validation set_slug(atts_to_slug)
end
def set_slug(atts)
slug_string = atts.map{|f| f.parameterize}.join("-")
# Then I would think I can do something like:
# model_instance.send("slug", slug_string)
end
end
end
Then I'd like to do something like this in the model:
class Classification < ApplicationRecord
include Sluggable
slug :name, :street
end
The problem I'm finding is:
How do I set the model's attribute slug from the module?