activerecord - Rails - Join on Condition -


i have 2 models,item , stock keeping unit

stock keeping units belong items , given item can have several stock keeping units each status of sold or not sold.

by definition -

  • in stock items items have stock keeping units unsold (sold = false)
  • out of stock items items have no stock keeping units unsold (sold = false)

for in stock items here scope -

    scope :in_stock, -> {       joins(:stock_keeping_units).distinct(:item).where('stock_keeping_units.sold = false')     } 

how scope "out of stock" items be? i've tried below, won't items have no stocks.

scope :out_of_stock, -> {     joins(:stock_keeping_units).where('stock_keeping_units.sold = true') } 

use in_stock scope:

 scope :out_of_stock, -> { where.not(id: in_stock.select('items.id')) } 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -