1
votes

I'm using friendly_id 4.0.0.beta8 and like to generate URLs that make use of slug, but still contain the model id. It seems like friendly_id generates slugs before_validation. However, at this time (before save) there is of course no model id available and so friendly_id creates slugs without id.

How can I make sure, that new records will have slugs with id after initial save?

2

2 Answers

7
votes

I'm the author of FriendlyId.

The problem that FriendlyId tries to solve is allowing text ids without the numeric id present at all.

So if you're just going to have the model id in the slug, you might just want to avoid the added complexity and not use FriendlyId at all for that model; you may be able to just override to_param:

def to_param
  "#{id}-{title.parameterize}"
end
0
votes

You could also just save twice in the controller. The ID will be there the second time so it'll update the slug.