ruby-on-rails – 如何指定Rails 3 scope.limit – 带偏移?
发布时间:2020-12-16 20:35:50 所属栏目:百科 来源:网络整理
导读:所以我有一些帖子,并希望在侧边栏中显示最近的条目(这些数字在配置中设置) 我可以轻松获取最新的n条记录 class Post ActiveRecord::Base default_scope :order = "created_at DESC" scope :published,lambda { where("blog_entries.created_at = ?",Time.zon
所以我有一些帖子,并希望在侧边栏中显示最近的条目(这些数字在配置中设置)
我可以轻松获取最新的n条记录 class Post < ActiveRecord::Base default_scope :order => "created_at DESC" scope :published,lambda { where("blog_entries.created_at <= ?",Time.zone.now) } scope :latest,lambda { |n| published.limit(n) } end @posts = Post.latest(6) 但我想要的是 @posts = Post.published.limit(6,12) 但是这个参数的数量是错误的,AR有什么办法吗?现在我正在玩will_paginate,但它似乎是hacky使用它为此. 解决方法
好的,所以答案是,我想:
@posts = Post.published.limit(6).offset(5) 它将从第六个开始检索6个职位. edit2:关于极限([6,12]),我觉得很奇怪: attr_accessor :limit_value def limit(value) relation = clone relation.limit_value = value relation end def build_arel ... arel.take(connection.sanitize_limit(@limit_value)) if @limit_value ... end def sanitize_limit(limit) if limit.is_a?(Integer) || limit.is_a?(Arel::Nodes::SqlLiteral) limit elsif limit.to_s =~ /,/ Arel.sql limit.to_s.split(',').map{ |i| Integer(i) }.join(',') else Integer(limit) end end 所以我真的不知道它如何与数组一起使用.但我显然错过了一些东西.你看到什么吗? 编辑:好尝试,但没有…;) 我不知道如果它会奏效,但是你尝试: @posts = Post.published.limit(6..12) ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |