Why is using the rails default_scope often recommend against?

后端 未结 5 2282
执笔经年
执笔经年 2020-11-28 03:59

Everywhere on the internet people mention that using the rails default_scope is a bad idea, and the top hits for default_scope on stackoverflow are

5条回答
  •  独厮守ぢ
    2020-11-28 04:18

    For me is not a bad idea but must be used with caution!. There is a case where I always wanted to hide certain records when a field is set.

    1. Preferably the default_scope must match with the DB default value (e.g: { where(hidden_id: nil) })
    2. When you are totally sure you want to show those records, there is always the unscoped method that will avoid your default_scope

    So it will depend and the real needs.

提交回复
热议问题