Skip to content

Instantly share code, notes, and snippets.

@ijonas
Created July 30, 2014 16:17
Show Gist options
  • Save ijonas/896e35cb6d0be921decb to your computer and use it in GitHub Desktop.
Save ijonas/896e35cb6d0be921decb to your computer and use it in GitHub Desktop.
Not using default scope
class Product < ActiveRecord::Base
validates :title, :description, :image_url, :presence=> true
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
validates :title, :uniqueness => true
validates :image_url, format: {
:with => %r{\.(gif|jpg|png)\Z}i,
:message => 'must be a url'
}
def ordered_by_title
order('title')
end
end
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
@products = Product.all.ordered_by_title
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment