Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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 Rails:从关联的多个子项中检索最新的时间戳_Ruby On Rails_Postgresql_Rails Activerecord - Fatal编程技术网

Ruby on rails Rails:从关联的多个子项中检索最新的时间戳

Ruby on rails Rails:从关联的多个子项中检索最新的时间戳,ruby-on-rails,postgresql,rails-activerecord,Ruby On Rails,Postgresql,Rails Activerecord,我的父对象有两个子对象 他们每个人都在时间戳上更新了_,并在时间戳上查看了_ i、 e.带有一些的person对象具有多个/属于多个关联 书籍:更新于,查看于 视频:更新于,查看于 我想查询这四次中最新的一次。 i、 e Person.getAllBooksAndVideos.Pulk:updated_at,:viewsed_at.sort{a,b|[a.updated_at,a.viewsed_at].compact.max[b.updated_at,b.viewsed_at].compact

我的父对象有两个子对象

他们每个人都在时间戳上更新了_,并在时间戳上查看了_

i、 e.带有一些的person对象具有多个/属于多个关联

书籍:更新于,查看于

视频:更新于,查看于

我想查询这四次中最新的一次。 i、 e

Person.getAllBooksAndVideos.Pulk:updated_at,:viewsed_at.sort{a,b|[a.updated_at,a.viewsed_at].compact.max[b.updated_at,b.viewsed_at].compact.max}.last
我不知道如何做GetAllBooks和Videos。Pulk:Update\u at,:viewed\u at part,任何建议都非常非常欢迎。

您可以将所有关联结果整理成一个数组,并调用sort by

结果=person.books+person.videos.sort_by{| x |[x.updated_at,x.viewsed_at]}。最后

然后您应该能够访问结果,如下所示: result.updated_此时应提供您的更新日期
result.viewed_at This应该提供您在日期查看的内容

我不确定,它是否有效,但您可以尝试此查询人员。加入:book,:videos.selectpersons.*,GREATESTbooks.updated_at,books.viewed_at FROM books,GREATESTvideos.updated_at,videos.viewed_at FROM videos