Forum: Ruby on Rails Find non-empty association?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2006-03-01 12:15
(Received via mailing list)
I have two models, Category and Article.  Article belongs_to :category
and Category has_many :articles.  I'd like to be able to find all
non-empty categories.  What's the best way to do that?

Pat
Bf66e10c8fc4abefebde0425e7f6f15a?d=identicon&s=25 Norman Timmler (Guest)
on 2006-03-01 12:42
(Received via mailing list)
Am Mittwoch, den 01.03.2006, 04:14 -0700 schrieb Pat Maddox:
> I have two models, Category and Article.  Article belongs_to :category
> and Category has_many :articles.  I'd like to be able to find all
> non-empty categories.  What's the best way to do that?

Category.find(
  :all,
  :include => :articles,
  :conditions => 'articles.category_id IS NOT NULL'
)

I think this might be the right clause. But i am not 100% sure.

--
Norman Timmler

http://blog.inlet-media.de
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2006-03-01 20:51
(Received via mailing list)
On 3/1/06, Norman Timmler <lists@inlet-media.de> wrote:
>
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

Thanks for the code bit.  Unfortunately I'm not sure if it works,
because I'm using HABTM.  Of course it was late and didn't ask the
proper question..but is there a nice way to do it for a HABTM?

Pat
This topic is locked and can not be replied to.