I need to create a class method of my ActiveRecord. But that method is not meant to be a scope, so I'd like to prevent misusage of it like a scope. Is it possible to do?
Asked
Active
Viewed 61 times
0
-
2You **can** create class methods by `def self.your_method_name` and mark it `private`. – kiddorails May 14 '13 at 13:43
-
Thanks. But I need them to be public. – Sergey Potapov May 14 '13 at 13:44
-
1Then IMHO, it is also prone to whatever misuse, you fear with a scope. – kiddorails May 14 '13 at 13:51
1 Answers
1
Like kiddorails hinted, this is not possible. The underlying reason is that AR puts many responsibilities into one class -- validation, persistence, finding/querying. If you really care about encapsulating these methods in one place, then you might consider creating a separate class (which would prevent 'misuse' by nature as it only has query-related methods) that handles just the query-related logic. (There are other strategies one could consider as well, too)

Brian Hahn
- 438
- 5
- 5