Not getting any love (tender or otherwise) on my pull
request<https://github.com/rails/rails/pull/7035>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')
.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.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.