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

Document how to generate slugs for existing documents #68

Closed
paulspringett opened this issue Jun 8, 2012 · 4 comments
Closed

Document how to generate slugs for existing documents #68

paulspringett opened this issue Jun 8, 2012 · 4 comments

Comments

@paulspringett
Copy link

I had a hard time working out how add a slug retrospectively. Given the model:

class Product
  include Mongoid::Document
  include Mongoid::Slug

  field :name
  field :slug

  slug :name
end

I initially tried calling save but this doesn't generate it.
The only way I could find to create slugs for existing documents was:

Product.all.each do |product|
  product.send(:generate_slug!)
  product.save
end

This seems a bit hacky... Is there a better way to do this?

If so could you add it to the README?
If this is the only way, could you expose a public method to generating slugs on existing documents?

@hakanensari
Copy link
Collaborator

Looks fine to me—as long as you don't have a large collection.

A somewhat shorter way to write could be:

Product.all.each &:to_param

See here.

This kind of stuff could go into the Wiki.

@paulspringett
Copy link
Author

@hakanensari to_param was what I was looking for - thank you! I'll submit a pull request to add this to the README

@monfresh
Copy link

I still don't see this documented in the README or in the Wiki.
.all.each &:to_param didn't work for me. This is what worked:

Product.all.each do |product|
  product.send(:set_slug)
  product.save
end

Is this the right way to do it?

@niketpuranik
Copy link

.all.each &:to_param didn't work for me. This is what worked:

Product.all.each do |product|
product.send(:build_slug)
product.save
end

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

4 participants