Check if association exists without incurring a database hit
问题 Is there a way to check if an ActiveRecord's belongs_to association exists without incurring a database query. I'm using example_association.present? to check and it results in the association being loaded if it does. All I want to know is if the association exists. 回答1: You could use reflect_on_all_associations as: Foo.reflect_on_all_associations(:belongs_to).map(&:name).include?(:example_assoc) Where :example_assoc is one of the belongs_to association. Or if you have an instance of model