Skip to content

Instantly share code, notes, and snippets.

@vitalyp
Last active August 29, 2015 13:57
Show Gist options
  • Save vitalyp/9426534 to your computer and use it in GitHub Desktop.
Save vitalyp/9426534 to your computer and use it in GitHub Desktop.
class CreatePrices < ActiveRecord::Migration
def change
create_table :prices do |t|
t.integer :price
t.integer :old_price
t.string :name
end
end
end
-----------------------------------------------
class Price < ActiveRecord::Base
validates_presence_of :price
validates_presence_of :old_price
validate :price_check
def price_check
if self.price > 10
self.errors.add(:price, 'price is baaad')
end
end
end
---------------------------------------------------
$ rails c
> p = Price.new
=> #<Price id: nil, price: nil, old_price: nil, name: nil>
> p.price = 12
=> 12
p.save!
(0.2ms) begin transaction
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
(0.1ms) rollback transaction
ActiveRecord::RecordInvalid: Validation failed: Old price can't be blank, Price price is baaad
RESULT
--------
Old Price validator executed. price_check validator executed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment