Rails: possui muitas associações - encontre com a condição AND, não OR condição
Tenho o seguinte método de consulta no meu modelo ActiveRecord:
def self.tagged_with( string )
array = string.split(',').map{ |s| s.lstrip }
select('distinct photos.*').joins(:tags).where('tags.name' => array )
end
Então, ele localiza todos os registros que possuem tags retiradas de uma lista separada por vírgulas e convertidas em uma matri
Atualmente, isso corresponde aos registros com QUALQUER tag correspondente - como posso fazê-lo funcionar onde corresponde a TODAS as tag
IE: se atualmente, se eu inserir: "azul, vermelho", então todos os registros serão marcados com azul OU vermelh
Quero corresponder a todos os registros marcados com azul e vermelho.
Suggestions?
- EDIT -
Meus modelos são assim:
class Photo < ActiveRecord::Base
...
has_many :taggings, :dependent => :destroy
has_many :tags, :through => :taggings
...
def self.tagged_with( string )
array = string.split(',').map{ |s| s.lstrip }
select('distinct photos.*').joins(:tags).where('tags.name' => array )
end
...
end
class Tag < ActiveRecord::Base
has_many :taggings, :dependent => :destroy
has_many :photos, :through => :taggings
end
class Tagging < ActiveRecord::Base
belongs_to :photo
belongs_to :tag
end
Uma tag possui dois atributos: ID e Nome (string