I have three models photo, post and comments. In photo there is a polymorphic association attachable_id and attachable_type column. I want to assign attachable_type value 1,2 for post and comments respectively.
I have added following code
Photo.rb
belongs_to :attachable, polymorphic: true
Post.rb
has_many :photos, -> { where(attachable_type: 1) }, as: :attachable, class_name: "Photo", dependent: :destroy
Comment.rb
has_many :photos, -> { where(attachable_type: 2) }, as: :attachable, class_name: "Photo", dependent: :destroy
But when I try to get the count of photos of a post using
self.photos.count
The query generated is
SELECT COUNT(*) FROM `photos` WHERE `photos`.`attachable_id` = 853 AND `photos`.`attachable_type` = 0 AND `photos`.`attachable_type` = 1
I don't know how photos
.attachable_type
= 0 added to the query. Please help me to avoid this from the query