0

Imagine the following route:

get ':slug/:location'  

where :search is triggering a finder on two different models (ie. Businnes, Person) based on the slug provided.

The problem I'm currently facing is that I couldn't find a way to check two different models for existing slugs while generating a new one.

tl;dr: I'm trying to avoid the situation where two different models are given the same slug.

Any help appreciated.

Grzegorz Kazulak
  • 424
  • 3
  • 7
  • 12

1 Answers1

0

What you want to do is run a custom validator in each of your models, this one would go in your Business model, for example:

before_save :unique_slug

private

def unique_slug
  self.slug = self.name + "-" + self.id unless
    self.find_by_slug(:slug).blank? and Person.find_by_slug(:slug).blank?
  end
end
Jonathan Bender
  • 1,911
  • 4
  • 22
  • 39
  • yep, but that's not how friendly-id behaves when it finds an already existing slug (and not exactly i had in mind). I want friendly-id to just generate a new one (ie. by appending uuid). – Grzegorz Kazulak Oct 26 '13 at 00:08
  • In that case, you can just have it set the ID to something else if the error occurs, I'll update my answer to have it do that. – Jonathan Bender Oct 26 '13 at 00:15