Skip to content

Instantly share code, notes, and snippets.

@foliea
Last active August 29, 2015 13:59
Show Gist options
  • Save foliea/10736378 to your computer and use it in GitHub Desktop.
Save foliea/10736378 to your computer and use it in GitHub Desktop.
class Page < ActiveRecord::Base
before_save :parameterize_name
validates :name, uniqueness: { scope: :locale }
validates :name, :locale, presence: true
scope :by_name, lambda { |name| where(name: name) }
def self.get_by_name! name
page ||= self.find_by(name: name, locale: I18n.locale)
page ||= self.find_by(name: name, locale: I18n.default_locale)
page ||= self.find_by!(name: name)
end
protected
def parameterize_name
self.name = name.parameterize
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment