0

So I have been trying to firstly check if the post's title is unique before posting it. In the model I added this line but it does not do what I expected:

validates_presence_of :title, uniqueness: true

Of course, I can check before making the post (in the controller) and if a post with this title exist to return an error message but is this the proper way to do it? I thought that there might be a validation for that.

2 Answers2

1

It should be

validates :title, uniqueness: true

or

validates_uniqueness_of :title
Santhosh
  • 28,097
  • 9
  • 82
  • 87
  • still the same. I get an article posted with title: testing and the second one: testing-f6daaf20-5cc4-41f6-9384-cf0410ced9a4 –  May 23 '14 at 12:56
1

It should look like this

validates :title, presence: true, uniqueness: true
Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
  • still the same. I get an article posted with title: testing and the second one: testing-f6daaf20-5cc4-41f6-9384-cf0410ced9a4 –  May 23 '14 at 12:55
  • 1
    I got your problem, are you using friendly_id slug on this column? – Rajdeep Singh May 23 '14 at 12:57