LoginSignup
2
2

More than 5 years have passed since last update.

Rails 4.0 の ActiveRecord::Relation のバグっぽい挙動?

Posted at

以下のような2つのscopeがあるActiveRecordのクラスがあるとする。

class Clip < ActiveRecord::Base 
  scope :display, -> { order("clips.updated_at DESC") }
  scope :undone, -> { where.not(status: 8) }
end

[7]の実行結果が理解できない。直すかバグ報告したい。Edgeでは確認していない。

[4] pry(main)> Clip.display # display scope 単独
  Clip Load (0.3ms)  SELECT "clips".* FROM "clips" ORDER BY clips.updated_at DESC
=> []
[5] pry(main)> Clip.undone# undone scope 単独
  Clip Load (0.3ms)  SELECT "clips".* FROM "clips" WHERE ("clips"."status" != 8)
=> []
[6] pry(main)> Clip.display.undone # display -> undone の順で呼び出す
  Clip Load (0.3ms)  SELECT "clips".* FROM "clips" WHERE ("clips"."status" != 8) ORDER BY clips.updated_at DESC
=> []
[7] pry(main)> Clip.undone.display # undone -> display の順序で呼び出すとnilになる。
#<ActiveRecord::Relation::ActiveRecord_Relation_Clip:0x007f80620108d0>=> nil
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2