Domanda

When creating RecommendPost, user enter a video_url, which is a youtube video link and triggers before_save :get_video_id, which is to process the video_url to get the video_id

For example, user creates @recommend_post, fill in https://www.youtube.com/watch?v=R9ITLdmfdLI, and then :get_video_id is triggered, and then we get the video_id R9ITLdmfdLI. At the end, all is saved into the @recommend_post

The problem is that, validates_uniqueness_of :video_id is not working here. (I want a youtube video could only be recommended once. ) I guess the validation is bypassed by the before_save method?

And how to validate the video_id?

È stato utile?

Soluzione

before_save is called and video_id is populated after validations are ran. In this case, you are validating the uniqueness of nil, which will pass if :allow_nil => true.

Use the before_validate callback to populate video_id before validating.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top