You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Failure/Error: it { should validate_presence_of(:title) }
NoMethodError:
undefined method to_slug for nil:NilClass
# ./app/models/article.rb:40:in generate_slug
# ./spec/models/article_spec.rb:39:in block (2 levels) in <top (required)>
The shoulda-matcher, validate_presence_of is passing nil to test presence validation, but it turns out, slug generation happens before validation and the error is happening because :title is nil. Does generate_slug really have to happen before validation or can we put it in another callback?
The text was updated successfully, but these errors were encountered:
The only reason generate_slug is done before validation is so that a slug can be generated on create and update (e.g. the user deleted the slug on update for it to re-generate a new one).
Feel free to add nil checking or some other approach as long as this behavior (generating slug on create and update) is implemented.
I'm trying to improve some of the model tests by using shoulda-matchers and I came across something interesting about slug generation.
I tried changing this part:
into this:
and I got this error:
The shoulda-matcher,
validate_presence_of
is passingnil
to test presence validation, but it turns out, slug generation happens before validation and the error is happening because:title
isnil
. Doesgenerate_slug
really have to happen before validation or can we put it in another callback?The text was updated successfully, but these errors were encountered: