Last active
August 29, 2015 13:57
-
-
Save vitalyp/9426534 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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