I have Rails polymorphic model and I want to apply different validations according to associated class.
I have Rails polymorphic model and I want to apply different validations according to
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The class name is in the
_typecolumn for instance in the following setup:the comment class is going to have the
commentable_idandcommentable_typefields.commentable_typeis the class name andcommentable_idis the foreign key. If you wanted to to a validation through comment for post-specific comments, you could do something like this:OR, and I think I like this better:
if you have several validations, I would recommend this syntax instead: