I want to count the number of elements in an array specific to a user id and then display that number. The last_viewed array keeps track of the event the user last visited.
The schema.db looks like this (its not the whole schema I just included what I think is necessary to solve my problem.)
create_table "users", id: :serial, force: :cascade do |t|
t.integer "last_viewed", default: [], array: true
end
I tried
<%= ((Users.last_viewed.where("id = ?", current_user.id)).count) %>
but it seems to think that last_viewed is a method and not part of the Users table.
I know how to use real SQL but these queries in Ruby on Rails are confusing. Essentially I want to turn this SQL code into something that works on Ruby.
SELECT array_length(u.last_viewed, 1)
FROM users u
WHERE u.id = <user's id>;
Users.where("id = ?", current_user.id)).last_viewed.countarray_lengthfunction so you can reference it in a model? The benefits would vary, but for a very large array you would not have to retrieve it to Rails to process it.