Rails: find attribute of sibling model?

So, I tried to find an example of how to do this, and I'm not sure I even know how to describe what I'm trying to do. I am a complete noob when it comes to SQL, and I'm sure this is really basic, but I completely lost:

I have a model Photo, which has_many :tags, :through => :taggings. Tags have a name and an identifier.

I want to do something like: Photo.where( #tag_name in [array] )

... but, as I said, I have no idea how to write something like this or what to look for to see an example on Google.

Can someone give me an example of such a request and what can it be called?

Thank!

+3
source share
2 answers

, PostgreSQL:

def self.tagged_with( string )
  array = string.split(',').map{ |s| s.lstrip }
  select('distinct photos.*').joins(:tags).where('tags.name' => array )
end

! . , , "".

+1

, , :

Photo.joins(:tags).where('tags.name' => ['herp','derp']).group(:id)

, API- AREL 3 , ActiveRecord, .

+9

All Articles