What is the recommended way to wrap an ActiveRecord accessor?
Given something like
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
I would like to be able to make it that, for example, @post.comments returns randomly sorted comments.
Of course I could create another method, like random_comments, but I would like to know if there’s a less error prone way (I don’t want to have to remember to call the random_comments method).
Calling super doesn’t work, as the comments method is created reflectively on the Post class and not inherited.
So how would you do it?
Hope this helps..
Now, calling
@post.commentsshould return comments in random order. But, remember, it would be an array and not an active relation.