Home > OS >  rails has many complex query
rails has many complex query

Time:09-09

I have multiple has_many associations in different models.

I am trying to get all those posts where asset_urls are empty.

class Post
 has_many :assets
 ...
end

class Asset
 has_many :asset_urls
 ...
end

class AssetUrl
 ...
end

What is the best and most optimized way to achieve those posts where asset_urls are nil/empty?

CodePudding user response:

Yes you can do something like Post.includes(:assets).where.missing(:asset_urls)

  • Related