Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate slug with a name and id of the record #1008

Open
GabrielLyonB opened this issue May 10, 2023 · 1 comment
Open

Generate slug with a name and id of the record #1008

GabrielLyonB opened this issue May 10, 2023 · 1 comment

Comments

@GabrielLyonB
Copy link

I want to generate the the slug with the name column and the id. So i set the slug_candidates like this:

def slug_candidates
  [
    [:name, :id]
  ]
end

The problem here is that the slug is set before the record is created. So when the slug is created the id is not yet set. I created an after_create custom method to unset the slug and then save again to regenerate the slug.

after_create :custom_set_slug

def custom_set_slug
  # skips validations on pourpose, so when is called the save method it sets the proper slug
  update_column(:slug, nil)
  save!
end

Is there a better way to do this?

@sergiotapia
Copy link

I have this exact same use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants