mongoid / mongoid-slug

Generates a URL slug/permalink based on fields in a Mongoid-based model.
https://github.com/mongoid/mongoid-slug
MIT License
492 stars 164 forks source link

Scoping not working under Rails 5 #233

Open cenotaph opened 7 years ago

cenotaph commented 7 years ago

Hi,

The docs for scoping say to use referenced_in and references_many but these options appear to have been deprecated in favour of more ActiveRecord-style belongs_to and has_many (I think--??).

Under Mongoid 6.0.2 and mongoid-slug 5.3.1 (master branch), the scope option doesn't work (at least when set up with belongs_to and has_many -- using referenced_in and references_many, as specified in the docs, throws an exception, so what am I doing wrong?

Saving a record with a duplicate slugged attribute (but a different scope) throws the dreaded 'E11000 duplicate key error index' error. I've tried explicitly defining the _id association as well, as suggested by the docs.

If scoping is still possible, could the README.md please be updated to indicate how to do this?

Thanks!

dblock commented 7 years ago

Do you think you can write a spec for this?

cenotaph commented 7 years ago

No, don't really know how to do that. Could the documentation just be corrected? Is there something beyond changing reference_in to belongs_to and references_many to has_many?

dblock commented 7 years ago

Maybe. Make a pull request and we can talk about it. I didn't dig any deeper into the issue, but I can try.

marceloboeira commented 7 years ago

Update

I had the same problem, updating to mongoid 6.0.3 solved!

That is working

A channel has several episodes:

Channel

class Channel
  include Mongoid::Document
  include Mongoid::Slug

  field :title, type: String

  slug :title

  validates :title, presence: true

  has_many :episodes
end

Episode

class Episode
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Slug

  field :title, type: String
  field :description, type: String
  field :url, type: String
  field :published_at, type: DateTime

  slug :title, scope: :channel

  validates :title, presence: true
  validates :url, presence: true
  validates :published_at, presence: true

  belongs_to :channel

end