The document discusses Active Record validations in Ruby on Rails. It provides an overview of validations and covers validation helpers like presence, uniqueness, length, format, numericality, acceptance, confirmation, inclusion, exclusion and validates_with. It also discusses validation callbacks, conditional validations, custom validations, displaying validation errors and more.
6 reasons Jubilee could be a Rubyist's new best friendForrest Chang
(Video here: http://confreaks.com/videos/5014-RubyConf2014-6-reasons-jubilee-could-be-a-rubyist-s-new-best-friend or https://www.youtube.com/watch?feature=player_embedded&v=FFR0G89WXI8)
Rubyconf 2014 talk on Jubilee, a Vert.x module that runs rack apps.
Alternate titles
Beyond Rails while using Rails
Rails can't do everything I want and <fill> makes me want to cry
Rubyconf abstract
Do you do web development in Ruby? Have you been forced to go to node or other technologies just for concurrency/websockets etc. Do miss your gems, and tire of functionality you have to implement from scratch? Do you hate javascript?
Well no need to switch languages/platforms, Jubilee could be your new best friend.
Jubilee, a rack server on top of Vert.x gives you
* Concurrency
* Speed
* Easy Websockets support
* Shared Memory
* Access to the JVM ecosystem
* Ability to reuse your existing Ruby knowledge and gems
"Say Hello to your new friend" - Al Pacino
6 reasons Jubilee could be a Rubyist's new best friendForrest Chang
(Video here: http://confreaks.com/videos/5014-RubyConf2014-6-reasons-jubilee-could-be-a-rubyist-s-new-best-friend or https://www.youtube.com/watch?feature=player_embedded&v=FFR0G89WXI8)
Rubyconf 2014 talk on Jubilee, a Vert.x module that runs rack apps.
Alternate titles
Beyond Rails while using Rails
Rails can't do everything I want and <fill> makes me want to cry
Rubyconf abstract
Do you do web development in Ruby? Have you been forced to go to node or other technologies just for concurrency/websockets etc. Do miss your gems, and tire of functionality you have to implement from scratch? Do you hate javascript?
Well no need to switch languages/platforms, Jubilee could be your new best friend.
Jubilee, a rack server on top of Vert.x gives you
* Concurrency
* Speed
* Easy Websockets support
* Shared Memory
* Access to the JVM ecosystem
* Ability to reuse your existing Ruby knowledge and gems
"Say Hello to your new friend" - Al Pacino
ActiveWarehouse/ETL - BI & DW for Ruby/RailsPaul Gallagher
Presentation delivered at the Singapore Ruby Brigade meetup 6-Jan-2010 (at hackerspace.sg). Discusses BI and DW in the Rails context, and test drives ActiveWarehouse and ActiveWarehouse/ETL with a "Cupcakes Inc" sample application.
- Ruby on Rails (RoR) is a platform of choice for emerging technology firms in eCommerce, Cloud, e-Business and mobile space.
- Neev is a leading provider of technology services in the emerging technology space and has developed a strong competency and resource pool with RoR skills.
- Neev also offers its RoR development services through turn-key projects or Outsourced Product Development platforms.
- Neev can help setup teams of 1 to 20 resources for a duration of 3 months to 12 months through a dedicated team model.
Introduction to Ruby on Rails by Rails Core alumnus Thomas Fuchs.
Originally a 3-4 hour tutorial, 150+ slides about Rails, Ruby and the ecosystem around it.
PDF, audio, and voiceover are now available on designintechreport.wordpress.com
Today’s most beloved technology products and services balance design and engineering in a way that perfectly blends form and function. Businesses started by designers have created billions of dollars of value, are raising billions in capital, and VC firms increasingly see the importance of design. The third annual Design in Tech Report examines how design trends are revolutionizing the entrepreneurial and corporate ecosystems in tech. This report covers related M&A activity, new patterns in creativity × business, and the rise of computational design.
Continuous Integration For Rails ProjectLouie Zhao
introduce continuous integration into the development process. choose integrity since it’s light-weighted, easy to configure, good support for git and works for ourselves on our own install.
My popular talk on Debugging WordPress, presented at WordCamp London, WordCamp Norrkoping, Software University and WPBGUG
Video: http://wordpress.tv/2014/05/23/mario-peshev-debugging-wordpress/
As present at FluentConf 2014 on March 11th, 2014.
AngularJS is one of the most popular, and powerful, JavaScript frameworks for building rich client-side applications. AngularJS is both simultaneously both simple to use and extremely full featured. With AngularJS a little goes a long way, but to make the most of it, you need to know what you’re doing.
In this workshop we will build a complex application to help exercise all of the salient points of the AngularJS framework.
Topics covered include, ngResource, directives, fitlers, routing, templates, controllers, testing, and more.
Code can be found at: https://github.com/markbates/fluent-2014
Have you ever cried yourself to sleep unable to find the cause of a horrendous bug in your WordPress site? Cry no more, your tears will be reshaped as blinding swords as we explore uncharted territories laced with mystical creatures.
Debugging is an often avoided topic due to the uncertainty of how best to accomplish it and the lack of powerful introspective tools. This talk will explore new territory and showcase tools that help you debug complex and difficult issues in your WordPress site.
Lessons learned from a huge Rails app - RubyConf Brasil 2019Nahuel Garbezza
On SpeakerDeck: https://speakerdeck.com/ngarbezza/lessons-learned-from-a-huge-rails-app-rubyconf-brasil-2019
A set of experiences working on a very challenging project, with a domain model constantly evolving, 24/7 availability, multiple teams working in the same codebase, integration with many external services, and so on.
In this talk we'll try go through these experiences, focusing on these questions: How do we approach new features? How do we deal with tech debt or any other risks? What can we do to speed up delivery? Is Rails helping us to achieve our goals? How should be our testing strategy? How do we handle production errors?
ActiveWarehouse/ETL - BI & DW for Ruby/RailsPaul Gallagher
Presentation delivered at the Singapore Ruby Brigade meetup 6-Jan-2010 (at hackerspace.sg). Discusses BI and DW in the Rails context, and test drives ActiveWarehouse and ActiveWarehouse/ETL with a "Cupcakes Inc" sample application.
- Ruby on Rails (RoR) is a platform of choice for emerging technology firms in eCommerce, Cloud, e-Business and mobile space.
- Neev is a leading provider of technology services in the emerging technology space and has developed a strong competency and resource pool with RoR skills.
- Neev also offers its RoR development services through turn-key projects or Outsourced Product Development platforms.
- Neev can help setup teams of 1 to 20 resources for a duration of 3 months to 12 months through a dedicated team model.
Introduction to Ruby on Rails by Rails Core alumnus Thomas Fuchs.
Originally a 3-4 hour tutorial, 150+ slides about Rails, Ruby and the ecosystem around it.
PDF, audio, and voiceover are now available on designintechreport.wordpress.com
Today’s most beloved technology products and services balance design and engineering in a way that perfectly blends form and function. Businesses started by designers have created billions of dollars of value, are raising billions in capital, and VC firms increasingly see the importance of design. The third annual Design in Tech Report examines how design trends are revolutionizing the entrepreneurial and corporate ecosystems in tech. This report covers related M&A activity, new patterns in creativity × business, and the rise of computational design.
Continuous Integration For Rails ProjectLouie Zhao
introduce continuous integration into the development process. choose integrity since it’s light-weighted, easy to configure, good support for git and works for ourselves on our own install.
My popular talk on Debugging WordPress, presented at WordCamp London, WordCamp Norrkoping, Software University and WPBGUG
Video: http://wordpress.tv/2014/05/23/mario-peshev-debugging-wordpress/
As present at FluentConf 2014 on March 11th, 2014.
AngularJS is one of the most popular, and powerful, JavaScript frameworks for building rich client-side applications. AngularJS is both simultaneously both simple to use and extremely full featured. With AngularJS a little goes a long way, but to make the most of it, you need to know what you’re doing.
In this workshop we will build a complex application to help exercise all of the salient points of the AngularJS framework.
Topics covered include, ngResource, directives, fitlers, routing, templates, controllers, testing, and more.
Code can be found at: https://github.com/markbates/fluent-2014
Have you ever cried yourself to sleep unable to find the cause of a horrendous bug in your WordPress site? Cry no more, your tears will be reshaped as blinding swords as we explore uncharted territories laced with mystical creatures.
Debugging is an often avoided topic due to the uncertainty of how best to accomplish it and the lack of powerful introspective tools. This talk will explore new territory and showcase tools that help you debug complex and difficult issues in your WordPress site.
Lessons learned from a huge Rails app - RubyConf Brasil 2019Nahuel Garbezza
On SpeakerDeck: https://speakerdeck.com/ngarbezza/lessons-learned-from-a-huge-rails-app-rubyconf-brasil-2019
A set of experiences working on a very challenging project, with a domain model constantly evolving, 24/7 availability, multiple teams working in the same codebase, integration with many external services, and so on.
In this talk we'll try go through these experiences, focusing on these questions: How do we approach new features? How do we deal with tech debt or any other risks? What can we do to speed up delivery? Is Rails helping us to achieve our goals? How should be our testing strategy? How do we handle production errors?
These are the slides presented at StarWest 2015 in Anaheim, CA. The slides are showcasing all of the advanced usages of selenium or add ons to improve your selenium test cases
Rails Antipatterns (http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814) 를 읽고 몇 가지 정도만 요약을 해보았습니다.
레일즈를 시작한지 얼마 되지 않은터라 맞지 않는 부분이 있더라도 양해 부탁드려요 :)
My talk from DevOpsCon Berlin 2019 about the lessons I've learned from agile software development that can be applied to infrastructure as code including Terraform unit testing and CloudFormation linting.
Accelerate your Kubernetes clusters with Varnish CachingThijs Feryn
A presentation about the usage and availability of Varnish on Kubernetes. This talk explores the capabilities of Varnish caching and shows how to use the Varnish Helm chart to deploy it to Kubernetes.
This presentation was delivered at K8SUG Singapore. See https://feryn.eu/presentations/accelerate-your-kubernetes-clusters-with-varnish-caching-k8sug-singapore-28-2024 for more details.
DevOps and Testing slides at DASA ConnectKari Kakkonen
My and Rik Marselis slides at 30.5.2024 DASA Connect conference. We discuss about what is testing, then what is agile testing and finally what is Testing in DevOps. Finally we had lovely workshop with the participants trying to find out different ways to think about quality and testing in different parts of the DevOps infinity loop.
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
The publishing industry has been selling digital audiobooks and ebooks for over a decade and has found its groove. What’s changed? What has stayed the same? Where do we go from here? Join a group of leading sales peers from across the industry for a conversation about the lessons learned since the popularization of digital books, best practices, digital book supply chain management, and more.
Link to video recording: https://bnctechforum.ca/sessions/selling-digital-books-in-2024-insights-from-industry-leaders/
Presented by BookNet Canada on May 28, 2024, with support from the Department of Canadian Heritage.
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
91mobiles recently conducted a Smart TV Buyer Insights Survey in which we asked over 3,000 respondents about the TV they own, aspects they look at on a new TV, and their TV buying preferences.
GraphRAG is All You need? LLM & Knowledge GraphGuy Korland
Guy Korland, CEO and Co-founder of FalkorDB, will review two articles on the integration of language models with knowledge graphs.
1. Unifying Large Language Models and Knowledge Graphs: A Roadmap.
https://arxiv.org/abs/2306.08302
2. Microsoft Research's GraphRAG paper and a review paper on various uses of knowledge graphs:
https://www.microsoft.com/en-us/research/blog/graphrag-unlocking-llm-discovery-on-narrative-private-data/
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
Are you looking to streamline your workflows and boost your projects’ efficiency? Do you find yourself searching for ways to add flexibility and control over your FME workflows? If so, you’re in the right place.
Join us for an insightful dive into the world of FME parameters, a critical element in optimizing workflow efficiency. This webinar marks the beginning of our three-part “Essentials of Automation” series. This first webinar is designed to equip you with the knowledge and skills to utilize parameters effectively: enhancing the flexibility, maintainability, and user control of your FME projects.
Here’s what you’ll gain:
- Essentials of FME Parameters: Understand the pivotal role of parameters, including Reader/Writer, Transformer, User, and FME Flow categories. Discover how they are the key to unlocking automation and optimization within your workflows.
- Practical Applications in FME Form: Delve into key user parameter types including choice, connections, and file URLs. Allow users to control how a workflow runs, making your workflows more reusable. Learn to import values and deliver the best user experience for your workflows while enhancing accuracy.
- Optimization Strategies in FME Flow: Explore the creation and strategic deployment of parameters in FME Flow, including the use of deployment and geometry parameters, to maximize workflow efficiency.
- Pro Tips for Success: Gain insights on parameterizing connections and leveraging new features like Conditional Visibility for clarity and simplicity.
We’ll wrap up with a glimpse into future webinars, followed by a Q&A session to address your specific questions surrounding this topic.
Don’t miss this opportunity to elevate your FME expertise and drive your projects to new heights of efficiency.
The Art of the Pitch: WordPress Relationships and SalesLaura Byrne
Clients don’t know what they don’t know. What web solutions are right for them? How does WordPress come into the picture? How do you make sure you understand scope and timeline? What do you do if sometime changes?
All these questions and more will be explored as we talk about matching clients’ needs with what your agency offers without pulling teeth or pulling your hair out. Practical tips, and strategies for successful relationship building that leads to closing the deal.
11. Conditional Validation
:if and :unless
• A Symbol : a method name
• A String : a really short condition
• A Proc : an inline condition
Grouping conditions : with_options
ROR Lab.
12. Custom Validations
• Custom validators modules
: inherited from Two
★ ActiveModel::Validator
★ ActiveModel::EachValidator
★ Get the “record” argument as a
parameter
• Custom validation methods
• Custom validation helpers
ROR Lab.
13. Working with
Validation Errors
• errors
• errors.messages
• errors.full_messages( or errors.to_a)
• errors[:attr] : for a specific attribute
• errors.add(:attr, message)(or errors[:attr]=)
• errors[:base] : object’s state as a whole
• errors.clear : intentionally to clear
• errors.size : count of errors
ROR Lab.
14. Displaying Validation
Errors in the View
★ gem ‘dynamic_form’ ★ Error Messages CSS
.field_with_errors
#errorExplanation
#errorExplanation h2
#errorExplanation p
#errorExplanation ul li
ROR Lab.
15. Validation Errors : https://github.com/joelmoss/dynamic_form
Error CSS
#error_explanation
#error_explanation h2
#error_explanation p
<%= form_for(@product) do |f| %>
<% if @product.errors.any? %>
<div id="error_explanation"> #error_explanation ul li
<h2><%= pluralize(@product.errors.count, "error") %>
prohibited this product from being saved:
</h2>
<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %> .field_with_errors
generated by scaffold
apps/assets/stylesheets/scaffolds.css.scss
ROR Lab.
16. Validation Errors : https://github.com/joelmoss/dynamic_form
Error CSS
#error_explanation
<%= form_for(@product) do |f| %>
#error_explanation h2
<% if @product.errors.any? %> #error_explanation p
<div id="error_explanation">
<h2><%= pluralize(@product.errors.count, "error") %>
#error_explanation ul li
prohibited this product from being saved:
</h2>
<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li> .field_with_errors
<% end %>
</ul>
</div>
<% end %>
generated by scaffold
apps/assets/stylesheets/scaffolds.css.scss
ROR Lab.
18. Validation Errors :
‘dynamic_form’
• f.error_messages or
• error_messages_for :product
<%= form_for(@product) do |f| %>
<%= f.error_messages %>
<% end %>
ROR Lab.
19. Validation Errors : https://github.com/joelmoss/dynamic_form
‘dynamic_form’
generated by dynamic_form
r_ tag
e
:h ead :header_message
:message
<%= f.error_messages
:header_message => "Invalid product!",
:message => "You'll need to fix the following fields:",
:header_tag => :h3 %>
ROR Lab.
20. Validation Errors : https://github.com/joelmoss/dynamic_form
Error HTML
config/initializers/custom_error_message_html.rb field object
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
errors = Array(instance.error_message).join(',')
unless html_tag =~ /^<label/
%(#{html_tag}<span class="validation-error"> #{errors}</span>).html_safe
else
%(#{html_tag}).html_safe
end
end
#{html_tag} .validation-error
ROR Lab.
21. Validation Errors : https://github.com/joelmoss/dynamic_form
Error HTML
<% if @product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>
</div>
<% end %>
ROR Lab.
33. Validation Helpers :
acceptance
• a checkbox : a real or virtual attribute
• “must be accepted”
class Person ActiveRecord::Base
validates :terms_of_service, :acceptance = true
class Person ActiveRecord::Base
validates :terms_of_service,
:acceptance = { :accept = 'yes' }
ROR Lab.
34. Validation Helpers :
validates_associated
• should if associated with other models
• “is invalid”
class Library ActiveRecord::Base
has_many :books
validates_associated :books
• should use on one end of your
associations
ROR Lab.
35. Validation Helpers :
confirmation
• a virtual attribute appended with
“_confirmation”
• “doesn’t match confirmation”
class Person ActiveRecord::Base
validates :email, :confirmation = true
validates :email_confirmation, :presence = true
%= text_field :person, :email %
%= text_field :person, :email_confirmation %
ROR Lab.
36. Validation Helpers :
exclusion
• not included in a given set
:any enumerable object
• “is reserved”
class Account ActiveRecord::Base
validates :subdomain,
:exclusion = {
:in = %w(www us ca jp),
:message = Subdomain %{value} is reserved. }
ROR Lab.
37. Validation Helpers :
format
• match a given regular expression
• “is invalid”
class Product ActiveRecord::Base
validates :legacy_code,
:format = { :with = /A[a-zA-Z]+z/,
:message = Only letters allowed }
ROR Lab.
38. Validation Helpers :
inclusion
• included in a given set
: any enumerable object
• “is not included in the list”
class Coffee ActiveRecord::Base
validates :size,
:inclusion = {
:in = %w(small medium large),
:message = %{value} is not a valid size
}
ROR Lab.
40. Validation Helpers :
length (2/2)
• options
- :wrong_length, :too_long, :too_short
- a placeholder - %{count}
class Essay ActiveRecord::Base
validates :content, :size = {
:minimum = 300,
:maximum = 400,
:tokenizer = lambda { |str| str.scan(/w+/) },
:too_short = must have at least %{count} words,
:too_long = must have at most %{count} words
}
ROR Lab.
41. Validation Helpers :
numericality (1/2)
• only numeric values
: (+/−) integer/floating point
• “is not a number”
class Person ActiveRecord::Base
validates :email, :confirmation = true
validates :email_confirmation, :presence = true
%= text_field :person, :email %
%= text_field :person, :email_confirmation %
ROR Lab.
42. Validation Helpers :
numericality (2/2)
class Player ActiveRecord::Base
validates :points, :numericality = true
validates :games_played,
:numericality = { :only_integer = true }
/A[+−]?d+Z/
•:greater_than ➡ “must be greater than %{count}”
•:greater_than_or_equal_to ➡ “must be greater than or equal to %{count}
•:equal_to ➡ “must be equal to %{count}”
•:less_than ➡ “must be less than %{count}”
•:less_than_or_equal_to ➡ “must be less than or equal to %{count}
•:odd ➡ “must be odd”
•:even ➡ “must be even”
ROR Lab.
43. Validation Helpers :
presence (1/2)
• empty or whitespaces
• “can’t be empty”
class Person ActiveRecord::Base
validates :name, :login, :email, :presence = true
end
class LineItem ActiveRecord::Base
belongs_to :order
validates :order_id, :presence = true
ROR Lab.
44. Validation Helpers :
presence (2/2)
• empty or whitespaces
• “can’t be empty”
✘
class Person ActiveRecord::Base
validates :is_admin, :presence = true
end a boolean field ➞ false.blank? is true
class Person ActiveRecord::Base
validates :is_admin,
:inclusion = { :in = [true, false] }
ROR Lab.
45. Validation Helpers :
uniqueness
• add_index :table_name, :column_name, unique = true
• “has already been taken”
class Account ActiveRecord::Base
validates :email, :uniqueness = true other
end attributes
class Holiday ActiveRecord::Base
validates :name, :uniqueness = { :scope = :year,
:message = should happen once per year }
end
class Person ActiveRecord::Base
validates :name,
:uniqueness = { :case_sensitive = false }
ROR Lab.
46. Validation Helpers :
validates_with (1/2)
• a separate class for validation
• no default validate error message
class Person ActiveRecord::Base
validates_with GoodnessValidator,[:if/:unless/:on]
end
class GoodnessValidator ActiveModel::Validator
def validate(record)
if record.first_name == Evil
record.errors[:base] This person is evil
end
end
ROR Lab.
47. Validation Helpers :
validates_with (2/2)
• any additional options ➞ options
class Person ActiveRecord::Base
validates_with GoodnessValidator, :fields =
[:first_name, :last_name]
end
class GoodnessValidator ActiveModel::Validator
def validate(record)
if options[:fields].any?{|field| record.send(field) == Evil }
record.errors[:base] This person is evil
end
end
end
ROR Lab.
48. Validation Helpers :
validates_each
• no predefined validation function
➞a block
• no default error message
class Person ActiveRecord::Base
validates_each :name, :surname do |record, attr, value|
record.errors.add(attr, 'must start with upper case') if value =~ /A[a-z]/
end
end
ROR Lab.
49. Common Validation Options :
:allow_nil
class Coffee ActiveRecord::Base
validates :size,
:inclusion = { :in = %w(small medium large),
:message = %{value} is not a valid size },
:allow_nil = true
ROR Lab.
50. Common Validation Options :
:allow_blank
• nil or an empty string
class Topic ActiveRecord::Base
validates :title,
:length = { :is = 5 },
:allow_blank = true
end
Topic.create(title = ).valid? # = true
ROR Lab.
51. Common Validation Options :
:on
• when the validation should happen
class Person ActiveRecord::Base
# it will be possible to update email with a duplicated value
validates :email, :uniqueness = true, :on = :create
# it will be possible to create the record with a non-numerical age
validates :age, :numericality = true, :on = :update
# the default (validates on both create and update)
validates :name, :presence = true, :on = :save
ROR Lab.
52. Conditional Validation
:if :unless (1/4)
• Using a Symbol : a method name
class Order ActiveRecord::Base
validates :card_number, :presence = true,
:if = :paid_with_card?
def paid_with_card?
payment_type == card
end
ROR Lab.
53. Conditional Validation
:if :unless (2/4)
• Using a String : a really short condition
class Person ActiveRecord::Base
validates :surname, :presence = true,
:if = name.nil?
ROR Lab.
54. Conditional Validation
:if :unless (3/4)
• Using a Proc : an inline condition
class Account ActiveRecord::Base
validates :password, :confirmation = true,
:unless = Proc.new { |a| a.password.blank? }
a model
object
ROR Lab.
55. Conditional Validation
:if :unless (4/4)
• grouping conditional validations
condition
object
class User ActiveRecord::Base
with_options :if = :is_admin? do |admin|
admin.validates :password, :length = { :minimum = 10 }
admin.validates :email, :presence = true
end
ROR Lab.
56. Custom Validations :
Custom Validators (1/2)
• to extend ActiveMode::Validator
• to validate the state of whole record
class MyValidator ActiveModel::Validator
def validate(record)
unless record.name.starts_with? 'X'
record.errors[:name] 'Need a name starting with X please!'
end
end
end
class Person
include ActiveModel::Validations
validates_with MyValidator
end
ROR Lab.
57. Custom Validations :
Custom Validators (2/2)
• to extend ActiveMode::EachValidator
• to validate individual attributes
class EmailValidator ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})z/i
record.errors[attribute] (options[:message] || is not an email)
end
end
end
EmailValidator
class Person ActiveRecord::Base
validates :email, :presence = true, :email = true
end
ROR Lab.
58. Custom Validations :
Custom Methods (1/2)
• to verify the state of models
class Invoice ActiveRecord::Base
validate :expiration_date_cannot_be_in_the_past,
:discount_cannot_be_greater_than_total_value
def expiration_date_cannot_be_in_the_past
if !expiration_date.blank? and expiration_date Date.today
errors.add(:expiration_date, can't be in the past)
end
end EmailValidator
def discount_cannot_be_greater_than_total_value
if discount total_value
errors.add(:discount, can't be greater than total value)
end
end
end
ROR Lab.
59. Custom Validations :
Custom Methods (2/2)
• to verify the state of models
class Invoice ActiveRecord::Base
validate :active_customer, :on = :create
def active_customer
errors.add(:customer_id, is not active)
unless customer.active?
end
ROR Lab.
60. Custom Validations :
Custom Helpers
• to reuse in several different models
• to put in config/initializers
ActiveRecord::Base.class_eval do
def self.validates_as_choice(attr_name, n, options={})
validates attr_name,
:inclusion = { { :in = 1..n }.merge!(options) }
end
end
class Movie ActiveRecord::Base
validates_as_choice :rating, 5
end
ROR Lab.
61. Validation Errors :
errors
• an instance of ActiveModel::Errors
• key : attribute name
value : an array of strings with all errors
class Person ActiveRecord::Base
validates :name, :presence = true, :length = { :minimum = 3 }
end
person = Person.new
person.valid? # = false
person.errors
# = {:name =
[can't be blank, is too short (minimum is 3 characters)]}
person = Person.new(:name = John Doe)
person.valid? # = true
person.errors # = []
ROR Lab.
62. Validation Errors :
errors[ ]
• to check the error message of a specific
attribute
class Person ActiveRecord::Base
validates :name, :presence = true, :length = { :minimum = 3 }
end
person = Person.new(:name = John Doe)
person.valid? # = true
person.errors[:name] # = []
person = Person.new(:name = JD)
person.valid? # = false
person.errors[:name] # = [is too short (minimum is 3 characters)]
person = Person.new
person.valid? # = false
person.errors[:name]
# = [can't be blank, is too short (minimum is 3 characters)]
ROR Lab.
63. Validation Errors :
errors.add (1/2)
• to manually add messages of a specific
attribute
class Person ActiveRecord::Base
def a_method_used_for_validation_purposes
errors.add(:name, cannot contain the characters !@#%*()_-+=)
end
end
person = Person.create(:name = !@#)
person.errors[:name]
# = [cannot contain the characters !@#%*()_-+=]
person.errors.full_messages
# = [Name cannot contain the characters !@#%*()_-+=]
ROR Lab.
64. Validation Errors :
errors.add (2/2)
-or-
class Person ActiveRecord::Base
def a_method_used_for_validation_purposes
errors[:name] = cannot contain the characters !@#%*()_-+=)
end
end
person = Person.create(:name = !@#)
person.errors[:name]
# = [cannot contain the characters !@#%*()_-+=]
person.errors.to_a
# = [Name cannot contain the characters !@#%*()_-+=]
ROR Lab.
65. Validation Errors :
errors[:base]
• related to the object’s state as a whole
• an array
class Person ActiveRecord::Base
def a_method_used_for_validation_purposes
errors[:base] This person is invalid because ...
end
end
ROR Lab.
66. Validation Errors :
errors.clear
• related to the object’s state as a whole
• an array
class Person ActiveRecord::Base
validates :name, :presence = true, :length = { :minimum = 3 }
end
person = Person.new
person.valid? # = false
person.errors[:name]
# = [can't be blank, is too short (minimum is 3 characters)]
person.errors.clear
person.errors.empty? # = true
p.save # = false
p.errors[:name]
# = [can't be blank, is too short (minimum is 3 characters)]
ROR Lab.
67. Validation Errors :
errors.size
class Person ActiveRecord::Base
validates :name, :presence = true, :length = { :minimum = 3 }
end
person = Person.new
person.valid? # = false
person.errors.size # = 2
person = Person.new(:name = Andrea, :email = andrea@example.com)
person.valid? # = true
person.errors.size # = 0
ROR Lab.