Question: I have two models name `album` and `albumphoto` :

```
class Album < ActiveRecord::Base
    has_many :albumphotos, dependent: :destroy
end

class Albumphoto < ActiveRecord::Base
    has_many :album, dependent: :destroy
end

```

Now I need to get the number of `Albums` which don't have any `albumphotos` in them. How can I do this?

Answer: 
Your schema is wrong because there can't be bidirectional `has_many`. You should use `has_many` and `belongs_to` if a photo can belong only to one album or other relation like `has_many :through` or `has_and_belongs_to_many` if one photo can belong to different albums. For `belongs_to` you can use `LEFT JOIN`:

```
Album.joins('LEFT JOIN albumphotos ON albumphotos.album_id = albums.id').
    where('albumphotos.id' => nil)

```

For `has_many :through` and `has_and_belongs_to_many` it'll be almost the same but there'll be third table like `albums_to_photos`.
Assuming you have a albumphoto\_id in your albums table, and if your Albums don't have any albumphotos in them means albumphotos\_id = nil in your albums table, you can do this

```
Album.where(albumphotos_id: nil).count

```