C. T. Cutler
2006-Feb-12 20:16 UTC
[Rails] ActiveRecord Many-To-Many relationship question
Hello, 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 list 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 include tag foo), I just don''t know how to get around it short of iterating through the list of photos returned by photo.find and re-queyring for each one individually. Thanks in advance! C.T. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060212/d80c7b85/attachment.html
@photos = Tag.find_by_name(''foo'').photos Will return all photos from the ''foo'' tag. Joey -- Posted via http://www.ruby-forum.com/.
Anthony Green
2006-Feb-13 16:58 UTC
[Rails] Re: ActiveRecord Many-To-Many relationship question
joey__ wrote:> @photos = Tag.find_by_name(''foo'').photos > Will return all photos from the ''foo'' tag. > > > Joeythen.. for photo in @photos ... for tag in photo.tags tag.name end end possibly ? Tony -- Posted via http://www.ruby-forum.com/.