首先在 model 中定义类方法进行查询
class Post < ActiveRecord::Base
belongs_to :user
has_many :photos
# belongs_to :sub
accepts_nested_attributes_for :photos, allow_destroy: true
def self.main(main)
where(main: main)
end
def self.sub(sub)
where(sub: sub)
end
def self.create_before(date)
where("created_at < ?", date)
end
def self.create_after(date)
where("created_at > ?", date)
end
end
要查询的是在以时间为比较查询这个时间之前(后)各 N 条数据,并以降序的顺序来返回 json 数组,接口中调用如下:
resource :posts do
desc 'post_list'
params do
requires :access_token, :type => String, :desc => "token"
requires :main, :type => String, :desc => "main"
requires :sub, :type => String, :desc => "sub"
requires :date, :type => DateTime, :desc => "date"
requires :count, :type => Integer, :desc => "count"
end
post 'posts_lists' do
if Post.count == 0
error!({status: false, :errors => ["posts count is zero"] }, 201)
end
begin
if params[:count] > 0
posts = Post.main(params[:main]).sub(params[:sub]).create_after(params[:date]).limit(params[:count])
else
posts = Post.main(params[:main]).sub(params[:sub]).create_before(params[:date]).order(created_at: :desc).limit(params[:count].abs)
end
status(201)
{
status: true,
errors:[],
posts: posts.select(:id,:title,:created_at,:previewurl,:replycnt,:upcount,:downcount)
}
rescue ActiveRecord::RecordNotFound
error!({status: false, :errors => ["not find posts"] }, 201)
end
end
发现问题这里当 当我查询查询
posts = Post.main(params[:main]).sub(params[:sub]).create_after(params[:date]).limit(params[:count])
当 count > 0 查询结果是出来是以按照升序的方式排列 但是当我把查询结果以降序排序:
{
"posts": [
{
"id": 21,
"title": "Sed excepturi explicabo fugit est sapiente culpa alias iusto.",
"created_at": "2015-07-19T11:38:38.000Z",
"previewurl": "http://filestorageapp.b0.upaiyun.com/uploads/user_profile/avatar/3/normal_avatar.jpg",
"upcount": 20,
"downcount": 20,
"replycnt": 20
},
{
"id": 22,
"title": "Quibusdam modi impedit veniam rem quisquam consectetur odit aut.",
"created_at": "2015-07-19T11:39:38.000Z",
"previewurl": "http://filestorageapp.b0.upaiyun.com/uploads/user_profile/avatar/3/normal_avatar.jpg",
"upcount": 21,
"downcount": 21,
"replycnt": 21
},
{
"id": 23,
"title": "Fuga consequuntur reiciendis unde vero quidem non inventore.",
"created_at": "2015-07-19T11:40:38.000Z",
"previewurl": "http://filestorageapp.b0.upaiyun.com/uploads/user_profile/avatar/3/normal_avatar.jpg",
"upcount": 22,
"downcount": 22,
"replycnt": 22
},
{
"id": 24,
"title": "Eius earum ipsum harum et rerum et officia totam.",
"created_at": "2015-07-19T11:41:38.000Z",
"previewurl": "http://filestorageapp.b0.upaiyun.com/uploads/user_profile/avatar/3/normal_avatar.jpg",
"upcount": 23,
"downcount": 23,
"replycnt": 23
},
{
"id": 25,
"title": "Et deleniti voluptas quo inventore maiores.",
"created_at": "2015-07-19T11:42:38.000Z",
"previewurl": "http://filestorageapp.b0.upaiyun.com/uploads/user_profile/avatar/3/normal_avatar.jpg",
"upcount": 24,
"downcount": 24,
"replycnt": 24
}
]
}
posts = Post.main(params[:main]).sub(params[:sub]).create_after(params[:date]).limit(params[:count]).order(created_at: :desc)
输出结果则是从整张表最上面查找出来结果进行排序,这个结果就完全跟之前查询目的不一样,而是排序后再进行时间比较过后的查询结果 结果如下:
{
"status": true,
"errors": [],
"posts": [
{
"id": 100,
"title": "Enim repudiandae sed quaerat dolorum soluta pariatur eum velit cupiditate.",
"created_at": "2015-07-19T12:57:38.000Z",
"previewurl": "http://filestorageapp.b0.upaiyun.com/uploads/user_profile/avatar/3/normal_avatar.jpg",
"upcount": 99,
"downcount": 99,
"replycnt": 99
},
{
"id": 99,
"title": "Eum aut eius repellat mollitia ullam rerum perspiciatis veritatis doloremque.",
"created_at": "2015-07-19T12:56:38.000Z",
"previewurl": "http://filestorageapp.b0.upaiyun.com/uploads/user_profile/avatar/3/normal_avatar.jpg",
"upcount": 98,
"downcount": 98,
"replycnt": 98
},
{
"id": 98,
"title": "Molestias fugiat fugit incidunt doloribus.",
"created_at": "2015-07-19T12:55:38.000Z",
"previewurl": "http://filestorageapp.b0.upaiyun.com/uploads/user_profile/avatar/3/normal_avatar.jpg",
"upcount": 97,
"downcount": 97,
"replycnt": 97
},
{
"id": 97,
"title": "Maxime maiores voluptatum deleniti assumenda aut dicta.",
"created_at": "2015-07-19T12:54:38.000Z",
"previewurl": "http://filestorageapp.b0.upaiyun.com/uploads/user_profile/avatar/3/normal_avatar.jpg",
"upcount": 96,
"downcount": 96,
"replycnt": 96
},
{
"id": 96,
"title": "Sint autem nemo facere ad odit quidem ullam voluptatem.",
"created_at": "2015-07-19T12:53:38.000Z",
"previewurl": "http://filestorageapp.b0.upaiyun.com/uploads/user_profile/avatar/3/normal_avatar.jpg",
"upcount": 95,
"downcount": 95,
"replycnt": 95
}
]
}
我不知道有什么排序接口不影响最后查询结果来进行排序 求大神指点迷津