Friendly_id character using UUID, although it is overloaded

I have a model, and according to the friendly_id gem, it looks like this:

class FinancialYear < ApplicationRecord
  extend FriendlyId
  friendly_id :slug_candidates, use: :slugged

  def slug_candidates
    [
        :end_year,
        [:end_year, :max_id]
    ]
  end

  def should_generate_new_friendly_id?
    self.slug.blank? || self.year_changed?
  end

  def end_year
    if !self.year.nil? && self.year.length > 1
      self.year.split('-')[-1].strip
    else
      self.year
    end
  end

  def max_id
    FinancialYear.where(year: end_year).count + 1
  end

end

What he had to do was to include the year: β€œ1999-2000” in the pool: β€œ2000” and 2000-2 ... etc. to avoid collisions.

Unfortunately, my tests did not materialize: "2000", received: "2000-f7608e8b-a2e7-449c-ae54-4785c7a68dec"

I use friendly_id on a different model in my application, and I use the same technique, and its work is excellent. Any help or suggestions as to why this does not work would be greatly appreciated.

UPDATE After several experiments, I found that this seems to only happen in my rspec tests, but I don’t understand why? Any thoughts?

+4
1

, slug, .

###delete all slug values
 User.update_all(:slug=>nil) 

###re-run to get the new slug candidate affective overriding the default alpanumeric slug
 User.find_each(&:save)

, .

+3

Source: https://habr.com/ru/post/1669325/


All Articles