Specifying a special column in ActiveRecord rails?

Let's say we do:

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

in the model, we can access woah as a method on the model, but this is the line. How can we do this so that it is an integer?

In my real example, I actually select the identifier from the joined table, but it is typed as a string. I need it to be an integer in a ruby.

+3
source share
1 answer

How to use a read-only virtual attribute in your model:

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

def woah
  raw_woah.to_i
end
+6
source

Source: https://habr.com/ru/post/1711893/


All Articles