Not getting any love (tender or otherwise) on my pull request so I thought you folks might like to chip in. What do you think?
#cast and #cast! allow you to declare a mapping from generated column names to simplified
database column types (i.e. those used in migrations). This way scopes with
columns generated in their select lists can also include the information as to what
form that data takes. This saves the user from having to translate the data
from a sql string wherever it’s used.
Here’s an example of where I’d use this:
class Topic < ActiveRecord::Base has_many :replies scope :by_most_recently_replied, joins(:replies).group('[topics.id](http://topics.id)') .select('topics.*, MAX(replies_topics.written_on) AS latest_reply_written_on') .order('latest_reply_written_on DESC').cast(latest_reply_written_on: :datetime) end >> topic = Topic.by_most_recently_replied.first >> topic.latest_reply_written_on.class => Time
The cast is applied to postgres columns only when there is no OID info for the column.
What do you guys think? Remaining work is to test handling of binary data - not quite sure how to go about that but I don’t expect any surprises.