我需要有关执行以下操作的查询的帮助:
另外只是为了让您知道,这是通过$ .ajax启动的。
原因是因为我正在将新的+旧记录实时发送到客户端。想想像用户开始访问网站之类的东西,它会从新的开始获取当前记录。然后,用户可以获取较旧的记录,但在同一请求下,它还会检索全新的记录。只有一次只有16条记录。
我有道理吗?
这是我目前的代码:
RssEntry.includes(:RssItem).where("rss_items.rss_id in (?) AND (rss_entries.id < ? OR rss_entries.created_at > ?)", rssids, lid, time).order("rss_entries.id DESC").limit(16)
lid = last lowest id from those records
rssids = ids from where to get the records
time = last time it did the records call
上面的代码只是一个开始。我现在需要帮助以确保它符合我的要求。
更新1
好的,所以我设法做了我想要的但是在2个SQL查询中。我真的认为不可能在一个SQL查询中做我想做的事。
非常感谢任何帮助。
感谢。
答案 0 :(得分:3)
首先,使用范围来获得你想要的东西:
class RssEntry < ActiveRecord::Base
scope :with_items, includes(:RssItem)
scope :newer_first, order("rss_entries.created_at DESC")
def self.in(args)
where(id: args)
end
def self.since(time)
where('rss_entries.created_at > ?', time)
end
end
然后
RssEntry.with_items.in(rssids).since(time).offset(lid).limit(16).newer_first
它应该按预期工作。