#945 ✓resolved
Ted Han (knowtheory)

Finders lose relational information across M:M relationships, and as a result fail to group & return properly.

Reported by Ted Han (knowtheory) | July 3rd, 2009 @ 07:31 AM | in 0.10.2

example:

    @presentations = 10.of{ Presentation.gen }
    @presentations.each_with_index do |presentation,index| 
      presentation.tag_list = %w(a b c d e f g h i j)[0+1..index].join(" ")
      presentation.save
    end

so:

    >> @presentations.map{ |p| p.tags.map{ |t| t.id }}
    => [[], [2 ], [2, 3], [2, 3, 4], [2, 3, 4, 5], [2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], [2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9, 10]]

However:

    >> Presentation.all(:id => @presentations.map{|p| p.id }).map{ |p| p.tags.map{ |t| t.id } }
    => [[2 ], [3 ], [4 ], [5 ], [6 ], [7 ], [8 ], [9 ], [10 ], []]

Same thing happens if you just do Presentation.all.map{ |p| p.tags.map{ |t| t.id }}, but the above example better indicates how incorrect the result is.

As a result,

    >> @presentations.map{ |p| p.tags } == Presentation.all(:id => @presentations.map{ |p| p.id }).map{ |p| p.tags }
    => false

Why is this happening? Well the following DM query:

    >> Presentation.all(:id => @presentations.map{|p| p.id }).map{ |p| p.tags.map{ |t| t.id } }

Produces the SQL below:

    SELECT "tags"."id", "tags"."name" FROM "tags"
            INNER JOIN "presentation_taggables" ON "tags"."id" = "presentation_taggables"."tag_id" 
            INNER JOIN "presentations" ON "presentation_taggables"."presentation_id" = "presentations"."id"
    WHERE "presentation_taggables"."presentation_id" IN (2, 3, 4, 5, 6, 7, 8, 9, 10, 11) 
    GROUP BY "tags"."id", "tags"."name" 
    ORDER BY "tags"."id"

The issue at hand is that the query should GROUP BY "tags"."id", "tags"."name", "presentation_taggables"."presentation_id" not just by "tags"."id", "tags"."name"

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Pages