views:

422

answers:

1

Let's say we do:

default_scope :select => '*, 1+1 AS woah'

in a model, we can then access woah as a method on the model, but it's a string. How do we typecast this so that it's an integer?

In my real-world example I'm actually selecting an id from a joined table but it's being typed as a string. I need it to be a ruby integer.

+6  A: 

How about using a read-only virtual attribute in your model:

default_scope :select => '*. 1+1 AS raw_woah'

def woah
  raw_woah.to_i
end
John Topley
Thanks John, that would do the trick :)
Brendon Muir