Undefined, where for # <Array: 0x007fd619b022b8>

I am trying to select an item from a cached array, but it does not work. I do the following:

> t=Task.last
=> #<Task id: 26, title: "Soloff" created_at: "2017-12-11 13:48:17", updated_at: "2017-12-11 15:57:12"> 
> t.cached_reminds 
 => [#<TaskRemind id: 3, deleted_state: false, task_id: 26, user_id: 1, date: "2017-12-27 23:00:00", created_at: "2017-12-11 16:28:34", updated_at: "2017-12-11 16:28:34">, #<TaskRemind id: 2, deleted_state: false, task_id: 26, user_id: 1, date: "2017-12-28 23:00:00", created_at: "2017-12-11 16:27:16", updated_at: "2017-12-11 16:27:16">] 

So my query displays a nice array, but after that, when I try to run:

t.cached_reminds.where(user_id: 1)

An action that is not recognized

Can you help me?

EDIT:

Create my model Task:

def cached_reminds
  Rails.cache.fetch([self, "task_reminds"]) {task_reminds.to_a}
end

Oddly enough, when I try to run:

t.task_reminds.where(user_id: 1)

It works!!

+4
source share
2 answers

task_reminds, , TaskRemind::ActiveRecord_Associations_CollectionProxy, where. , - :

def cached_reminds
  Rails.cache.fetch([self, 'task_reminds']) { task_reminds }
end

last_task = Task.last.task_reminds
last_task.cached_reminds
# => => TaskRemind::ActiveRecord_Associations_CollectionProxy
last_task.cached_reminds.where user_id: 1
+3

cached_reminds , ActiveRecord where

ruby ​​ select {} AR where. ,

t.cached_reminds.select { |cached| cached.user_id == 1 }
#=> An array of TaskRemind records or empty array

, , Array#select {}

+1

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


All Articles