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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user