Skip to content

Instantly share code, notes, and snippets.

@nyaahara
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyaahara/bf2084ec2e7b5432d0fa to your computer and use it in GitHub Desktop.
Save nyaahara/bf2084ec2e7b5432d0fa to your computer and use it in GitHub Desktop.
orderするとwhereで絞り込んだレコードが復活しちゃう
class Hoge < ActiveRecord::Base
has_many :fugas
end
class Fuga < ActiveRecord::Base
belongs_to :hoge
end
# statusがnullのレコードだけが取得
Hoge.all.includes(:fugas).references(:fugas).where('fugas.status is null').first.fugas
# statusがnull以外のレコードも取得
Hoge.all.includes(:fugas).references(:fugas).where('fugas.status is null').first.fugas.order(:id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment