Collections and to_partial_path
Rendering collections of Active Record models is really quite clean. Calling:
Effectively calls:
Internally, the render
method calls to_partial_path
on each item of the
collection to figure out which partial to render (in this case, posts/post).
ActiveRecord models inherit a method which returns a partial name based on the
name of the model. However, it is possible to overwrite said behavior. Plain
Ruby objects are able to implement the same behaviour.
Lets use this fact to render out an event feed. Each event has a type
column
(comment, star, upload, etc.). Each event in the feed is displayed differently,
and has a corresponding partial in the events
folder. Instead of doing a
switch on type in the view or model, we can take advantage of to_partial_path
to dynamically figure out the correct partial to render.