How to retrieve objects that have an attached file and order those objects based on a set of pre-ordered values?

别来无恙 提交于 2019-12-12 03:47:44

问题


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. 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;

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.

How can I make that?


回答1:


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')")


来源:https://stackoverflow.com/questions/10646374/how-to-retrieve-objects-that-have-an-attached-file-and-order-those-objects-based

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!