Skip to content

Instantly share code, notes, and snippets.

@francois-blanchard
Last active March 16, 2016 15:45
Show Gist options
  • Save francois-blanchard/e7e957648f8dbc3f7437 to your computer and use it in GitHub Desktop.
Save francois-blanchard/e7e957648f8dbc3f7437 to your computer and use it in GitHub Desktop.
Scope for get objects with no association

Scope for get objects with no association

class User < ActiveRecord::Base
  has_many :messages
  scope :without_messages, -> { where.not(id: Message.select(:user_id).uniq.compact) }
end

class Message < ActiveRecord::Base
  belongs_to :user
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment