Tag: Rails4

FriendlyId Ruby on Rails gem – update (refresh) slug after each record change

If you want to have slugs that are refreshed (updated) each time you change corresponding field in your ActiveRecord models, put this at the end of your initializers/friendly_id.rb file:

module FriendlyId
  module Slugged
    def should_generate_new_friendly_id?
      return true if send(friendly_id_config.slug_column).nil? && !send(friendly_id_config.base).nil?

      change = :"#{friendly_id_config.base}_changed?"
      return true if respond_to?(change) && send(change)

      false
    end
  end
end

This will update slug each time related column (friendly_id_config.base) has changed. Keep in mind, that this method won't work if you use non-column slug method as a base for friendly ids, since it uses ActiveRecord::Dirty interface.

State machine gem: undefined method underscore for ActiveModel::Name

If  you encounter this issue:

Failure/Error: ::Mobile::App::Talk.make!
NoMethodError:
  undefined method `underscore' for #<ActiveModel::Name:0x0000000ebae880>

When using state machine, machinist and machinist_mongo, please add this to your app/config/initializers/state_machine.rb:

# It seems that there is no underscore method on ActiveModel name
# It is added here, so it will be accessible and state machine
# can work. It should be removed after this is fixed
class ActiveModel::Name
  def underscore
    to_s.underscore
  end
end

This should be enough to make it work :)

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑