I have 3 models : Users has_one Service and Users Has_many Phones (verified or not, destroyed or not)
Class Phone < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_one :service
has_many :phones
has_many :verified_phones, -> {where('verified_at IS NOT NULL AND destroyed_at IS NULL')}, class_name: 'Phone'
end
class Service < ActiveRecord::Base
belongs_to :user
has_many :phones, through: :user
has_many :verified_phones, through: :user
end
I'd like to define an TS-index on the model Service. I want an boolean-faceted attribute which represents Service whose user has one or more verified phone.
So after reading this post I tried :
join verified_phones
has 'COUNT(verified_phones.id) > 0', as: :user_phone_verified, type: :boolean, facet: true
but send me the error "Unknown column 'verified_phones.id' in 'field list' " (the same error occured in the post)
Then I tried
join verified_phones
has 'COUNT(phones.id) > 0', as: :user_phone_verified, type: :boolean, facet: true
-> but result are wrong : attribute is true for every user that have a phone, verified or not, destroyed or not.
Then I tried
join phones
has 'COUNT(phones.verified_at IS NOT NULL AND phones.destroyed_at IS NULL) > 0', as: :user_phone_verified, type: :boolean, facet: true
-> same problem : attribute is true for every user that have a phone, verified or not, destroyed or not.
I'm not good with SQL syntax, could anybody help me to solve this ? Thanks