Add method find_by_slug_or_id to Sluggable module

Make it easier to find by slug or id for sluggable models. Will return
nil if resource is not found.
This commit is contained in:
Julian Herrero
2019-01-17 10:39:55 +01:00
parent 9a23393535
commit 3bf2fa1b17
2 changed files with 5 additions and 1 deletions

View File

@@ -25,7 +25,7 @@ module Budgets
end
def load_budget
@budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budgetid])
@budget = Budget.find_by_slug_or_id params[:budget_id]
end
def investments_by_heading_ordered_alphabetically

View File

@@ -3,6 +3,10 @@ module Sluggable
included do
before_validation :generate_slug, if: :generate_slug?
def self.find_by_slug_or_id(slug_or_id)
find_by_slug(slug_or_id) || find_by_id(slug_or_id)
end
end
def generate_slug