Forum: Ruby on Rails ActiveRecord Many-To-Many relationship question

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
F2d0b859b14896c81b34301431e2dfec?d=identicon&s=25 C. T. Cutler (Guest)
on 2006-02-12 21:16
(Received via mailing list)

I've got two models: Photo and Tag, which have an M to N relationship.

When I run Photo.find and specify "tag = foo" as a condition, I get a
of all photos associated with tag foo.  So far so good.

But each the tags member of each of the photo objects returned by
Photo.findcontains only the tag "foo" even if the photo in question
also has the tags
"bar" and "baz".  Is there some way to get ActiveRecord to return "foo",
"bar", and "baz" in the tags list?

I understand why I'm seeing this behavior (the underlying query joins
photos, photos_tags, and tags, and throws out all rows which don't
tag foo), I just don't know how to get around it short of iterating
the list of photos returned by photo.find and re-queyring for each one

Thanks in advance!

6dab365a82517fb694650a57ee88e4a4?d=identicon&s=25 joey__ (Guest)
on 2006-02-12 21:25
@photos = Tag.find_by_name('foo').photos
Will return all photos from the 'foo' tag.

F59329dc91cba06600ff65c85fd3e93c?d=identicon&s=25 Anthony Green (acgreen)
on 2006-02-13 17:58
joey__ wrote:
> @photos = Tag.find_by_name('foo').photos
> Will return all photos from the 'foo' tag.
> Joey


for photo in @photos
  for tag in photo.tags

possibly ?

This topic is locked and can not be replied to.