Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...