friendly_id: Breaks validates uniqueness: true
This seems to be the same issue as reported in the closed issue #152
My Gem file:
ruby '2.2.0'
gem 'rails', '4.2.0'
gem 'friendly_id', '~> 5.1.0'
Here is my model code:
validates :title, presence: true, uniqueness: true
extend FriendlyId
friendly_id :title, :use => :slugged
The
presence: true
validation works well, but my app does not apply the uniqueness validation… it will happily make model instances with duplicate :titles.
Elsewhere in my app, in models where I am not using Friendly_ID, I find the uniqueness validations work well.
Also – the slugging behavior and other features of Friendly_id do work as advertised.
thanks
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 4
- Comments: 16 (1 by maintainers)
Hey @norman, I’m building a simple rails api with basic token authentication and authorization with the pundit gem. I’m using your gem for friendly urls but I’m running into this same issue. Here’s the code. I can’t link to the repo because it’s private but I’m not giving anything away here.
config/initializers/friendly_id.rb
I’m building the methods instead of sending them because I read somewhere it’s faster??? Who knows, but it works for what I’m doin? The default_candidates just allows me to dry up my models a bit since in this particular app they all have a similar structure.
client.rb
RSpec model_spec.rb
Failure Message
it fails because, from what I’ve read from your previous versions, slug generation happens on a before validation callback. But shouldn’t that mean that the validation spec’s should be working? Looking at the stack trace I’m thinking it’s an issue with databasecleaner maybe but it’s hard to tell? Adding pry calls or byebugs to shoulda-matchers to poke around is usually time consuming in these cases.
My uniqueness test is passing which I know is slightly different from what’s happening in the title.
All this said, is it even relevant to be testing the slug column given that the gem is being tested on it’s own? Meaning if the functionality is there and the plugin itself is vetted, isn’t testing that it’s working in my code similar to testing Devise?
I’m mainly asking because I’m wondering if I even need to test it?