Forum: Ruby on Rails HABTM: Records with no parents

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.
932b1ebc80e0e5dcb63e6d3b29bc6ab3?d=identicon&s=25 Razorcom (Guest)
on 2006-01-24 00:45
(Received via mailing list)
I have a habtm relationship between two models.  What I want to do is
find items that aren't related.  So like if you had authors <->
posts, how would you find authors who don't have any posts?  Or posts
that don't have an author ?

Thanks,

Alex Strand

[astrand@razorcom.com]
Razorcom
www.razorcom.com

------------------------------------------------------------

Our company, Razorcom, is developing a product called LiquiPages.
LiquiPages is a way for content owners to maintain their websites and
publish changes from a centralized location.  If you are interested
in helping us with our beta test, please visit http://
www.liquipages.com/

------------------------------------------------------------

Visit the Razorcom Blog,  Revolution Trigger,  at:
http://www.razorcom.com/blog/
E8a360dd29cc74a16393eb515d34a181?d=identicon&s=25 Timo (Guest)
on 2006-01-24 01:12
(Received via mailing list)
Hi there.
Hope I get you right. Is this what you want?

authors.each do |author|
  if author.posts.empty?
    do_stuff
  end
end
932b1ebc80e0e5dcb63e6d3b29bc6ab3?d=identicon&s=25 Razorcom (Guest)
on 2006-01-24 01:18
(Received via mailing list)
Well I think actually backwards... like, what posts have no authors?

Thanks,

Alex Strand

[astrand@razorcom.com]
Razorcom
www.razorcom.com

------------------------------------------------------------

Our company, Razorcom, is developing a product called LiquiPages.
LiquiPages is a way for content owners to maintain their websites and
publish changes from a centralized location.  If you are interested
in helping us with our beta test, please visit http://
www.liquipages.com/

------------------------------------------------------------

Visit the Razorcom Blog,  Revolution Trigger,  at:
http://www.razorcom.com/blog/
68af18e151871f9573fad957f1147487?d=identicon&s=25 Benoit Gagnon (Guest)
on 2006-01-24 06:32
(Received via mailing list)
On 2006-01-23 19:16:47 -0500, Razorcom
<mrstrand@gmail.com> said:

> Well I think actually backwards... like, what posts have no authors?

I would strongly recommend agaisnt this method, which is very consuming
in terms of processing. If your DBMS allows subqueries (mySQL 4.1+
does, iirc), try the following:

Post.find :all, :conditions => "id NOT IN (SELECT DISTINCT post_id FROM
authors_posts)"

This will give you an array of all Post objects whose ids do not show
up in the join table.

The opposite (authors without any posts):

Author.find :all, :conditions => "id NOT IN (SELECT DISCINT author_id
FROM authors_posts)"

This technique is MUCH faster, and you still get full-featured
ActiveRecords objects. Don't fear SQL when it can really help out :)

Note that you could also use a counter cache (see Rails api doc) if
subqueries are not available.

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