This is a draft cheat sheet. It is a work in progress and is not finished yet.
Active Record: Query
User.all |
User.find(params[:id]) |
User.find_by(name: params[:name]) |
User.where(email: params[:email]) |
User.where("sign_in_count > 1") |
User.where("age > 18").limit(5) |
User.where(gender: "M").order(:name) |
User.find_each(batch_size: 5000) |
@users.pluck(:email) |
Active Record: Agregation
Person.average(:age) |
Trade.maximum(:price) |
Order.group(:status).count |
Person.count(:all) |
Command Line
rails (s)erver |
rails (c)onsole |
rails (g)enerate |
rake db:migrate |
rake db:migrate:status |
rake routes |
Routes
root :to => 'pages#home_page' |
get '/patients/:id', to: 'patients#show' |
resources :photos |
namespace :admin do ... end |
Forms
form_for(@model) do |f| ... end |
link_to 'name', path |
f.label |
f.text_field |
f.password_field |
f.email_field |
f.submit |
|
|
Active Record: Create / Update
User.create(name: "David", occupation: "Code Artist") |
@user.update(name: "Dave") |
@user.destroy |
@user.save / @user.save! |
User.find_or_create_by |
User.where(some condition).first_or_create(params) |
Validations
validates :name, :presence => true |
validates :name, :length => { :minimum => 2 } |
validates :password, length: { in: 6..20 } |
validates :email, :uniqueness => true |
validates :count, :numericality => { :greater_than => 0 } |
validates :size, inclusion: { in: %w(small medium large) } |
Migrations
create_table :table do |t| ... end |
add_column :table, :field, :type |
change_column :table, :field, :type |
rename_column :table, :name, :new_name |
rename_table |
add_index |
Misc
before_action :method |
scope :published, -> { where(published: true) } |
Active Record: Data Types
string |
float |
integer |
text |
time |
date |
boolean |
Active Record: Associations
:has_one |
:has_many / :belongs_to |
|