I have a SQL query in a Rails model that responds with an array of objects. And each object has an attribute called points with a value.
But its preferable for query to just return an array of points like [10,15,5] instead of [object,object,object] which requires then extracting the points out into another array to be useful.
Model file
LAST_3_SELECT = "
SELECT
(
(data.ap / (data.apa * 1.0))
+
(data.vp / (data.vpa * 1.0))
)
/ 2 * 1.5 * data.level
AS points
FROM data
WHERE data.user_id = ?
GROUP BY data.id
ORDER BY data.created_at DESC
LIMIT 3
"
def self.last_3(user_id)
connection.select_all(sanitize_sql_array( [LAST_3_SELECT, user_id]), "last-3")
end
Is this possible to do in a query itself, or necessary to do in a method outside it?
I don't have much experience writing raw SQL queries into Rails methods so any guidance would be very appreciated.
.map(&:points), it returnedNoMethodError (undefined method 'points' for {"points"=>2.4000000000000004}:Hash):.map{ |row| row["points"] }