Skip to content

Commit

Permalink
add Blog::Post#last_within pseudo scope
Browse files Browse the repository at this point in the history
  • Loading branch information
fwolfst committed Oct 6, 2020
1 parent 49d602f commit 8da9a9f
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/blog/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ class Blog::Post < ApplicationRecord

has_one_attached :image

# Not using a scope here because of 'first'
def self.last_within days: 7.days
active.where('published_at > ?', DateTime.current - days)
.order(published_at: :desc)
.limit(1)
.first
end

def previous
Blog::Post.active.where("created_at < ?", created_at).order(created_at: :asc).where.not(id: id).first
end
Expand Down

0 comments on commit 8da9a9f

Please sign in to comment.