Closed calicoder closed 12 years ago
I think this is something that is just broken on the current version on git, but from the looks of things from my Mongoid 3 app, mongoid-slug appears to be storing the full slug history by default now, but has no convenience methods for looking up slugs. You can drop a pretty simple class method into your model, like:
def self.find_by_slug(slug)
self.find_by(:_slugs => slug)
end
Which seems to be doing the trick, for now. Would love to know if this method is missing by design now, or not?
I tried adding that to my model and it doesn't fix the error when saving the model with an updated slugged field:
>> m = Member.last
>> m.username = "badninja"
>> m.save
NoMethodError: undefined method `username' for "badninja":String
from /Users/andy/workspace/myapp/app/models/member.rb:51:in `block in <class:Member>'
from /Users/andy/.rvm/gems/ruby-1.9.3-p125@myapp/bundler/gems/mongoid-slug-0294c663c42b/lib/mongoid/slug.rb:111:in `call'
from /Users/andy/.rvm/gems/ruby-1.9.3-p125@myapp/bundler/gems/mongoid-slug-0294c663c42b/lib/mongoid/slug.rb:111:in `find_unique_slug_for'
from /Users/andy/.rvm/gems/ruby-1.9.3-p125@myapp/bundler/gems/mongoid-slug-0294c663c42b/lib/mongoid/slug.rb:272:in `find_unique_slug_for'
from /Users/andy/.rvm/gems/ruby-1.9.3-p125@myapp/bundler/gems/mongoid-slug-0294c663c42b/lib/mongoid/slug.rb:303:in `find_unique_slug'
from /Users/andy/.rvm/gems/ruby-1.9.3-p125@myapp/bundler/gems/mongoid-slug-0294c663c42b/lib/mongoid/slug.rb:256:in `build_slug'
I fixed my issue temporarily by adding the following instance method to the model. This is not ideal because I have to add to this every slugged model until the fix is released (assuming that this is a gem problem, not user error):
def slug
self._slugs.last
end
This fixes my problem.
>> m = Member.first
>> m.username = "karina"
=> "karina"
>> m.save
=> true
>> m.slug
=> "karina"
>> m._slugs
=> ["username1", "karina"]
I would love to know what the bigger issue is here though. Thanks!
@calicoder @thetron Same as before there is no slug history by default. However, all slugs are stored in a single variable '_slugs' of Array type both when you are using the slug history and not. The '_slugs' field is indexed.
On master documents can be found by slug using the find(method). For instance, to find a document with the slug 'thetron' type Book.find('thetron').
@thetron I have to look into the last issue later since I am on vacation. The code looks fine at first glance.
Same problem here.
I'm using slug without history:
slug :name
Problem:
c = Category.new
c.name = "Test 1"
c.save
c.name = "Test 2"
c.save
#<Category _id: 504f252fc18564a702000001, _type: "category", created_at: 2012-09-11 11:49:24 UTC, updated_at: 2012-09-11 11:49:29 UTC, _slugs: ["test-1", "test-2"], parent_id: nil, parent_ids: [], position: 2, name: "Test 2", code: nil, source_id: nil, hidden: false, product_ids: []>
Category.find("test-1") => true
Category.find("test-2") => true
Hi Guys. Try my fork: https://github.com/tomaswitek/mongoid-slug
@tomaswitek Thank you for your fixes. I have merged them with master.
@calicoder @thetron @palodelincak Any slugged model now responds to the 'slug' method. The slug method returns the latest slug.
@thetron After merging @tomaswitek changes slug history is now disabled by default, and only enabled when the history flag is provided. Thank you for reporting this bug.
Slug seems to be undefined for a model attribute that has been slugged.
Also, I get this error when trying to set the username, which is slugged.
This is the model:
This is my gemlock:
Mongoid Slug seems to creating the :_slug attribute on the model:
Do you know what I'm doing wrong? Thank you!