Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

Grabbing all the has_many relations of a specific collection

General • Asked by Chris Zempel

I've got:

class User < ActiveRecord::Base
  has_many :photos

class Photo < ActiveRecord::Base
  belongs_to :user

Each user has a "shadowbanned" attribute (t/f). I'd like to be able to grab all the photos where the users aren't shadowbanned.

How would you recommend I set up either the scope or the association to have Rails do the most work possible?

Guess what I'm asking is, is there anything cleaner than this:

    valid_photos = Photo.where.not(user_id: User.where(shadowbanned: true).pluck(:id))

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 29,763+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.