如何检索具有附加文件,并命令基于一组$ P $对有序值的那些对象的对象? [英] How to retrieve objects that have an attached file and order those objects based on a set of pre-ordered values?

查看:234
本文介绍了如何检索具有附加文件,并命令基于一组$ P $对有序值的那些对象的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Ruby on Rails 3.2.2和2.7.0回形针。我想从数据库中检索其中的记录/对象有一个附加的文件(在我的情况下,它是图像文件)数据和命令基于一组pre-有序值的那些对象。也就是说,我有映像文件(由回形针宝石处理)和状态(状态可以是发表未公布阻止)的数据库表列,我希望是:

I am using Ruby on Rails 3.2.2 and Paperclip 2.7.0. I would like to retrieve data from the database where records/objects have an attached file (in my case it is an image file) and order those objects based on a set of pre-ordered values. That is, I have image_file_name (handled by the Paperclip gem) and status (status can be published, unpublished, blocked) database table columns and I would like that:

  1. 在返回的数据中包含的主要的记录/对象是关联的图像(也许这可以通过回形针宝石办理);
  2. 的对象检索在步骤1中的排序(在顺序的重要的),由阻止未公布然后发表状态;
  1. returned data contains primarily records/objects to which is associated an image (maybe this can be handled through the Paperclip gem);
  2. that objects retrieved at step #1 are sorted (in order of importance) by blocked, unpublished and then published statuses;

更多,如果返回的对象是小于5的话,我想追加对这些对象的其他对象,但不关心过滤器和排序为在步骤#1和#2。

More, if returned objects are less than 5, then I would like to "append" to those objects other objects but without caring "filters and ordering" as made in step #1 and #2.

我怎么能作出这样的?

推荐答案

这是数据存储的依赖性。如果你想通过一个字段的MySQL的具体数值命令,比如,你可以这样做:

This is datastore-dependent. If you want to order by specific values of a field in MySQL, for example, you can do:

Model.where('image_file_name is not null').order("field('status', 'blocked', 'unpublished', 'published')")

这篇关于如何检索具有附加文件,并命令基于一组$ P $对有序值的那些对象的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆