Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未定义的方法,其中for_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 未定义的方法,其中for

Ruby on rails 未定义的方法,其中for,ruby-on-rails,ruby,Ruby On Rails,Ruby,我试图从缓存数组中选择一个元素,但它不起作用。我正在做以下工作: > 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: 2

我试图从缓存数组中选择一个元素,但它不起作用。我正在做以下工作:

> 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">] 
未被识别的操作

你能帮我吗

编辑:

形成我的模型任务:

def cached_reminds
  Rails.cache.fetch([self, "task_reminds"]) {task_reminds.to_a}
end
奇怪的是,当我尝试跑步时:

t.cached_reminds.where(user_id: 1)
t.task_reminds.where(user_id: 1)
它起作用了

缓存的\u似乎是一个记录数组,因此不能在其中使用ActiveRecord的查询方法

对于ruby的数组,可以使用select{}来表示与AR where类似的内容。请注意传递给方法的块

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

有关更多信息,请阅读并

您可以在块中返回缓存的任务提醒,而无需将其转换为数组,这样它将为您提供一个TaskEmbrand::ActiveRecord\u Associations\u CollectionProxy对象,您可以在何处使用。与使用数组不同,在数组中必须使用某些东西来过滤其中的元素:

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

你提醒了什么?这是一个cahe方法形成一个模型任务。是的,我已经发布了!凉的谢谢你的回答@yeuem1vannam。我不知道这个方法。谢谢你宝贵的帮助