davidb
davidb

Reputation: 8954

Count only datasets that are associated through HABTM

I have two models with a HABTM associaton called LegacyDatum and Category Now I want to count how many Objects of LegacyDatum are associated with one or more categories. When I call LegacyDatum.joins(:categories) that works but it also returns the datasets with no category. How can I only get the datasets that have at least one category associated?

The Model is really simple, no unconventional associations... LegacyDatum:

class LegacyDatum < ActiveRecord::Base
        has_and_belongs_to_many :categories
        .....
        ....
        ...
end

Upvotes: 2

Views: 111

Answers (1)

Peter Brown
Peter Brown

Reputation: 51707

I believe it's because all is loading everything before the joins method is called. What do you get if you drop the all portion, or tack it on the end instead?

Upvotes: 0

Related Questions