12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails4でscopeにlambdaを付け忘るとチェインが外れる

Last updated at Posted at 2013-10-02

Rails4ではscopelambdaを付けないと、チェインが外れる。


class User < ActiveRecord::Base

scope :active, where("active IS TRUE")

scope :from_latest, order("created_at DESC")

end

等としていると、


User.active.from_latest

等としたときにactiveのスコープが適用されない(と思う)。


class User < ActiveRecord::Base

scope :active, lambda{where("active IS TRUE")}

scope :from_latest, lambda{order("created_at DESC")}

end

としなければならない。
ワーニングが出なかったように思うので、Rails 3からの移行時はお気をつけを。

実際に付け忘れて、OUTER JOINで数万と数万のテーブルWHERE句なしにをつなげて全件取得し、酷いことになった。

12
12
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
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?