Skip to content

Instantly share code, notes, and snippets.

@b-murphy
Created September 10, 2014 15:51
Show Gist options
  • Save b-murphy/34c820619b291d2dee32 to your computer and use it in GitHub Desktop.
Save b-murphy/34c820619b291d2dee32 to your computer and use it in GitHub Desktop.
def self.find_or_next(id)
find(id)
rescue ActiveRecord::RecordNotFound
if (id == last.id + 1) then raise ActiveRecord::RecordNotFound; end
find(id+1)
end
@b-murphy
Copy link
Author

badcode.

@bestie
Copy link

bestie commented Sep 11, 2014

I'm not sure why you want this but I think you should use find_by(id: id) to prevent the RecordNotFound being raised.

def self.find_or_next(id)
  find_by(id: id) or find(id+1)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment