Is there anyway to add a find condition to all Active record models?
that is I would like this query
ExampleModel.find :all, :conditions=> ["status = ?", "active"]
to behave the same way as
ExampleModel.find :all
in every model
Thanks!!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could use
default_scope:If you want to use this in all your models, you can either subclass
ActiveRecord::Baseand derive from that in all your models (probably doesn’t work well with single-table inheritance):…or you could set the
default_scopeonActiveRecord::Baseitself (could be annoying if you decide that one model should not have this default scope):As mentioned by klochner in a comment, you may also want to consider adding a
named_scopetoActiveRecord::Base, namedactive, for example: