Skip to content

Instantly share code, notes, and snippets.

@yasaichi
Created April 19, 2015 13:30
Embed
What would you like to do?
Instance methods for ActiveRecord to fetch the "neighbor" records
module NeighborRecords
def previous_one(col_name = :id)
condition = self.class.arel_table[col_name].lt(public_send(col_name))
self.class.where(condition).order(col_name => :desc).first
end
def next_one(col_name = :id)
condition = self.class.arel_table[col_name].gt(public_send(col_name))
self.class.where(condition).order(col_name => :asc).first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment