Skip to content

Instantly share code, notes, and snippets.

@webzorg
Created May 27, 2019 08:43
Show Gist options
  • Save webzorg/fb77cb141e9ee01a017886681ed2d3dd to your computer and use it in GitHub Desktop.
Save webzorg/fb77cb141e9ee01a017886681ed2d3dd to your computer and use it in GitHub Desktop.
class User < ApplicationRecord
has_many :wallets
end
class Currency < ApplicationRecord
has_many :wallets
end
class Wallet < ApplicationRecord
belongs_to :currency
belongs_to :user
end
Currency.create(currency_symbol: "usd")
Currency.create(currency_symbol: "eur")
10.times do
user = User.create;
user.wallets.create(currency_symbol: "usd")
user.wallets.create(currency_symbol: "eur")
end
# Let's assume not all wallets were created and there's one eur wallet missing, what is the query to find it?
# Something like this but this is not working.
#
User.joins(wallets: :currency).where(currency_symbol: "eur", wallets: { currency: { id: nil } })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment