Rails Cheat Sheet: ActiveRecord Callbacks
Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic before or after an alteration of the object state.
Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations:
Creating an Object
before_validationafter_validationbefore_savearound_savebefore_createaround_createafter_createafter_save
Updating an Object
before_validationafter_validationbefore_savearound_savebefore_updatearound_updateafter_updateafter_save
Destroying an Object
before_destroyaround_destroyafter_destroy
Rails Cheat Sheet: Named Scopes
Rails 3 allows you to define queries with named scopes, examples below.
scope :all_lazy, select(‘*’)
scope :find_lazy, lambda{|id| where(primary_key => id)}
scope :all_ordered, all_lazy.order(“<column_name> ASC”)
Rails Cheat Sheet: AcitveRecord Validation Helpers
Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers provide common validation rules.
- acceptance: validates that a checkbox on the user interface was checked when a form is submitted.
- validated_associated: ensures valid? is called on associated models.
- confirmation: ensures two text fields contain exactly the same data. This validation creates a virtual attribute with “_confirmation” appended to the field name. Useful for email and password validations.
- inclusion: ensures an attribute’s value is within a given set. The set may be an enumerable object.
- exclusion: ensures an attribute’s value is not within a given set. The set may be an enumerable object.
- format: ensures an attribute’s value matches a regular expression specified using the :with option.
- length: ensures an attribute’s value falls within, under, over or is exactly the specified length.
- numericality: ensures an attribute’s value contains only numeric characters.
- presence: ensures an attribute’s value is not empty.
- absence: ensures an attribute’s value is empty.
- uniqueness: ensures an attribute’s value is unique right before it is saved. It does not create a unique constraint in the database so concurrent connections may create duplicates.
- validates_with: this helper passes the record to another class for validation.
- validates_each: this helper validates each attribute against a block.
Rails Cheat Sheet: ActiveRecord migrations
Active Record provides the following methods that perform common data definition tasks in a database independent way:
- add_column
- add_index
- change_column
- change_table
- create_table
- drop_table
- remove_column
- remove_index
- rename_column
To perform database specific operations use the execute method to run arbitrary SQL.
Luhn Validation Gem
Luhn_Validation is a ruby gem implementing the Luhn (Modulus 10) algorithm commonly used for credit card validation. This software can only be used to check if a given number passes the Luhn algorithm. In cannot determine if a given number belongs to a credit card that is legitimate, in service or in any way valid as it does not interact with any financial institutions.
Refer to the License for further details.
Installation
gem install luhn_validation
Use
Require the gem:
require 'luhn_validation'
Instantiate a validation object:
luhn_val = LuhnValidation.new
Validate the credit card number. Note that basic string sanitising is performed, acceptable formats include:
- Alpha character separated strings such as hyphens (4444-3333-2222-1111) or whitespace (4444 3333 2222 1111) etc.
- FIXNUM
valid? returns true if the number passes luhn validation, false otherwise.
luhn_val.valid?('4444-3333-2222-1111')
You can also access the validation object’s more recently processed number via the cc_dirty and cc_clean attributes.
luhn_val.cc_dirty # => '4444-3333-2222-1111'
luhn_val.cc_clean # => 4444333322221111
Recent Comments