So, today I found an interesting code, and it caught my attention. I did not think it was possible. Line of code:
@post.comments.approved_comments
It looks good, right? But in fact, what happens is that the approved_comments method is called on the Array object instance, which is returned by the has_many relation (i.e. the Message has many comments). Even more interesting is that approved_comments is a class method defined in the Comment model.
Which puzzles me a bit, I thought you could not relate the relationship, not directly, because the relationship returns an Array object, not an ActiveRecord::Relation object. I asked a question related to this before, and the answer I was given is that you can bind relationships using the scoped method or one of many methods that are added to the array due to the relationship (i.e., Section 4.3.1 Methods added by has_many ).
What puzzles me is that approved_comments is the method we wrote; this is not a method added by the has_many relationship method. So how is it possible that I can access it? Even more confusing, how can I access it from an Array object?
There is, of course, some kind of magic. I am wondering if anyone can point me to some documentation or if any experts will answer this. Not a big problem, because it works pretty well, but I would love to know how easy it is to educate myself.
Thanks for the help in advance!
code
Message
class Post has_many :comments end
A comment
class Comment def self.approved_comments where(:approved => true) end end
Using
@post.comments.approved_comments