开发者

Decorate an ActiveRecord during load

开发者 https://www.devze.com 2023-04-05 01:52 出处:网络
I have a Rails ActiveRecord model that I want to return decorated results when I perform a query using my_method, but only once the query has executed and returned results.

I have a Rails ActiveRecord model that I want to return decorated results when I perform a query using my_method, but only once the query has executed and returned results.

In other words:

MyModel.where(condition).my_method.where(another_condition)

Will return an ActiveRelation. My method my_method does not disturb the chaining of queries as long as the results are lazy loaded.

MyModel.where(condition).my_method.where(another_condition).all

Will return MyDecoratedResult.

One way I could do this would be to have my_method do the decoration upon call, and intelligently forward subsequent requests to it's contents (the ActiveRelation). I don't mind this way, but would prefer a technique that requires less intelligence.

Would like to hear some ideas about other ways to开发者_运维知识库 achieve my goal. Other than the way I've mentioned, I can only think to extend ActiveRelation.

Thanks! Max

0

精彩评论

暂无评论...
验证码 换一张
取 消