Skip to content

Instantly share code, notes, and snippets.

@ryuta-ito
Last active August 3, 2019 04:43
Show Gist options
  • Save ryuta-ito/380a502db43cf1c1a2113f6f4ed8108c to your computer and use it in GitHub Desktop.
Save ryuta-ito/380a502db43cf1c1a2113f6f4ed8108c to your computer and use it in GitHub Desktop.

条件で絞り込んだテーブルとのright outer join & includes

class Bill
  def self.with_company(bill_month)
    joins(sanitize_sql_array(['right outer join companies on companies.id = bills.company_id and bills.bill_month = ?', bill_month])).select('bills.*, companies.id as company_id').includes(:user)
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment