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

Scoping not working under Rails 5 #233

Open
cenotaph opened this issue Dec 28, 2016 · 4 comments
Open

Scoping not working under Rails 5 #233

cenotaph opened this issue Dec 28, 2016 · 4 comments
Labels

Comments

@cenotaph
Copy link

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
Copy link
Collaborator

dblock commented Dec 28, 2016

Do you think you can write a spec for this?

@dblock dblock added the bug? label Dec 28, 2016
@cenotaph
Copy link
Author

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
Copy link
Collaborator

dblock commented Dec 30, 2016

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
Copy link

marceloboeira commented Jan 4, 2017

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

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

No branches or pull requests

3 participants