Created
October 18, 2009 17:32
-
-
Save latortuga/212760 to your computer and use it in GitHub Desktop.
A bunch of handy ruby and rails snippets
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
# This is used in Rails migrations to update ActiveRecord after adding/removing a column from a table | |
# so that the column can be used in the migration. Doesn't take any arguments, will update the AR | |
# model that it is called on. | |
ActiveRecord#reset_column_information | |
# Reload rails env while in script/console | |
reload! | |
# Rails field types | |
# :type MySQL type options | |
:binary TINYBLOB, BLOB, MEDIUMBLOB, or LONGBLOB :limit => 1 to 4294967296 (default = 65536) | |
:boolean TINYINT(1) | |
:date DATE | |
:datetime DATETIME | |
:decimal DECIMAL :precision => 1 to 63 (default = 10), :scale => 0 to 30 (default = 0) | |
:float FLOAT | |
:integer INT :limit => 1 to 11 (default = 11) | |
:primary_key INT(11) AUTO_INCREMENT PRIMARY KEY | |
:string VARCHAR :limit => 1 to 255 (default = 255) | |
:text TINYTEXT, TEXT, MEDIUMTEXT, or LONGTEXT :limit => 1 to 4294967296 (default = 65536) | |
:time TIME | |
:timestamp DATETIME | |
# Rails migrations quick reference | |
create_table :table_name do |t| | |
t.column :name, :type, :options | |
t.timestamps # adds created_at and updated_at | |
end | |
drop_table :name | |
rename_table :old, :new | |
add_column :table, :name, :type, :options | |
remove_column :table, :name | |
rename_column :old, :new | |
change_column :table, :name, :new_type, :options | |
add_index :table, [:columns], :options | |
remove_index :table, :index_name | |
# Run tests with verbose output | |
rake test TESTOPTS=-v | |
# Run specific test file, the :units is required or it will try to run other tests too | |
rake test:units TEST="test/unit/client_test.rb" |
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
# Use a regex in square brackets to capture | |
beginning_numbers = "12345gist"[/^\d+/] # => "12345" | |
# Convert a UNIX timestamp to a Time object ... | |
Time.at(timestamp) | |
# ... and vice versa | |
Time.new(params).to_i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment