Notes posted by nhance
RSS feedNo way to use custom message
In what appears to be a bug, there appears to be no way to use a custom error message when using this validator.
Common Validator options
Most validators will support all of the following common options: (Through ActiveModel::Errors::CALLBACK_OPTIONS (http://apidock.com/rails/ActiveModel/Errors))
-
:if
-
:unless
-
:allow_blank
-
:allow_nil
Typo in example above
You aren’t mistaken. That is an error in the example with the block above. There’s an extra ‘(’ character.
Doesn't work for associations.
This method relies on #blank? to determine if the attribute is valid.
When you call #blank? on an ActiveRecord object, it returns true as long as there are no changes to the object.
So you can validate the base attribute (i.e.: product_id), but you’ll have no guarantee that it points to a valid record without your own validator.
Checks if attribute is equal to '1' by default
It’s easy to overlook the :accept option which dictates that the attribute shall be ‘1’, not ‘yes’, not true, but ‘1’ only for validation to pass.
For ‘1’ shall the value be, no more, no less. ‘1’ shall be the value thou shalt validate, and thy validation shall check for ‘1’. ‘2’ shalt not thou validate, neither ‘3’, nor ‘0’, excepting that thou shall then set the value to ‘1’. true is right out. Once the value ‘1’, being the value of the attribute named, then thou shall have validation and thy object shall be valid.
Doesn't return nil on empty array when param is given
This does not return nil if the array is empty and n is given.
[].shift(2) # => [] a = [] a.shift(2) # => [] a # => []
Argument Accepted
Accepts a single argument record_separator which is the character or string to chomp.
Why isn’t this shown in the method def at the top?
Argument Accepted
Accepts a single argument sep_string
Status Codes
Full detail: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
-
100 - Continue
-
101 - Switching Protocols
-
200 - OK
-
201 - Created
-
202 - Accepted
-
203 - Non-Authoritative Information
-
204 - No Content
-
205 - Reset Content
-
206 - Partial Content
-
300 - Multiple Choices
-
301 - Moved Permanently
-
302 - Found
-
303 - See Other
-
304 - Not Modified
-
305 - Use Proxy
-
306 - No Longer Used
-
307 - Temporary Redirect
-
400 - Bad Request
-
401 - Not Authorised
-
402 - Payment Required
-
403 - Forbidden
-
404 - Not Found
-
405 - Method Not Allowed
-
406 - Not Acceptable
-
407 - Proxy Authentication Required
-
408 - Request Timeout
-
409 - Conflict
-
410 - Gone
-
411 - Length Required
-
412 - Precondition Failed
-
413 - Request Entity Too Large
-
415 - Unsupported Media Type
-
416 - Requested Range Not Satisfiable
-
417 - Expectation Failed
-
500 - Internal Server Error
-
501 - Not Implemented
-
502 - Bad Gateway
-
503 - Service Unavailable
-
504 - Gateway Timeout
-
505 - HTTP Version Not Supported
Doesn't return nil if the object you try from isn't nil.
Note that this doesn’t prevent a NoMethodError if you attempt to call a method that doesn’t exist on a valid object.
a = Article.new a.try(:author) #=> #<Author ...> nil.try(:doesnt_exist) #=> nil a.try(:doesnt_exist) #=> NoMethodError: undefined method `doesnt_exist' for #<Article:0x106c7d5d8>
This is on Ruby 1.8.7 patchlevel 174
Be careful with float ranges
Pay close attention to the fact that the object passed to :in must be enumerable.
If you want to validate a ranking, the following won’t work:
validates_inclusion_of :rating, :in => (0.0..10.0)
Instead, you’ll want to use validates_numericality_of like this:
validates_numericality_of :rating, :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 10.0