Skip to content

Instantly share code, notes, and snippets.

@adrianmann
Created December 16, 2019 14:45
Show Gist options
  • Save adrianmann/9204353d4c798806038c8655638f591c to your computer and use it in GitHub Desktop.
Save adrianmann/9204353d4c798806038c8655638f591c to your computer and use it in GitHub Desktop.
# app/models/vendor.rb
class Vendor < ActiveRecord::Base
has_many :products
# Rest of the code …
end
# app/models/product.rb
class Product < ActiveRecord::Base
belongs_to :vendor
has_many :taxes
# Rest of the code …
end
# app/models/tax.rb
class Tax
belongs_to :product
# Rest of the code …
end
# Get the rates of the taxes applied to products prefixed by "Product 00" belonging the vendor with ID 41.
# vendor = Vendor.find 41
# items = Product.all.select { |item| item.account_id = vendor.id && item.description.match(/\AProduct\s00/) }
# ids = item.map(&:id)
# taxes = Tax.where(item_id: ids)
# rates = taxes.map(&:rate)
Tax.joins(products: :vendors)
.where(products: {"description like ?", "Product 00%"}, vendors: {id: 41})
.map(&:rate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment