How to apply different validation rule according to polymorphic association type (Rails)?
问题 I have Rails polymorphic model and I want to apply different validations according to associated class . 回答1: The class name is in the _type column for instance in the following setup: class Comment belongs_to :commentable, :polymorphic => true end class Post has_many :comments, :as => :commentable end the comment class is going to have the commentable_id and commentable_type fields. commentable_type is the class name and commentable_id is the foreign key. If you wanted to to a validation