Skip to content

Instantly share code, notes, and snippets.

@pootsbook
Created December 2, 2012 20:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pootsbook/4190871 to your computer and use it in GitHub Desktop.
Save pootsbook/4190871 to your computer and use it in GitHub Desktop.
Ignore records with no attached records
$options['joins'] = array(
array('table' => 'images_news_articles',
'alias' => 'ImagesNewsArticle',
'type' => 'left outer',
'conditions' => array(
'NewsArticle.id = ImagesNewsArticle.news_article_id'
)
),
array('table' => 'images',
'alias' => 'Image',
'type' => 'left outer',
'conditions' => array(
'ImagesNewsArticle.image_id = Image.id'
)
)
);
$options['conditions'] = array(
'Image.id IS NOT NULL'
);
$news_articles_with_images = $NewsArticle->find('all', $options);
@pootsbook
Copy link
Author

In Rails:

class NewsArticle
  scope :unimaged, lambda { includes(:images).where("images.id is null") }
end

@Audacious
Copy link

scope :imaged, lambda { includes(:images).where("images.id IS NOT NULL") }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment