Rails Cheat Sheet

Common commands
$ rails s
$ rake test
$ rspec
$ rake test test
$ ruby test/test_meme.rb
$ git remote -v
$ git push staging
$ git push prod

Figure out heroku dataase instance
$ heroku apps
$ heroku addons –app hidden-xxxx-xxxx | grep POSTGRES
$ heroku-postgresql:dev HEROKU_POSTGRESQL_ORANGE

Generators

https://agilewarrior.wordpress.com/2014/10/12/rails-generators-cheat-sheet/

> rails g scaffold Foo foo_string:string foo_text:text foo_integer:integer foo_float:float foo_decimal:decimal foo_timestamp:timestamp foo_time:time foo_date:date foo_binary:binary foo_boolean:boolean

> rake db:migrate

Types: :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean.

Migrations

Rename database column

$ rails g migration RenameAwsVideoColumn

class FixColumnName < ActiveRecord::Migration
  def change
    rename_column :table_name, :old_column, :new_column
  end
end

Stand alone migration
$ rails generate migration AddPartNumberToProducts

Add/Remove column migration
$ rails generate migration AddBuyerIdToTransactions part_number:string buyer_id:integer

class AddBuyerIdToTransactions < ActiveRecord::Migration
  def change
    add_column :transactions, :buyer_id, :integer
  end
end

Console
$ rails console –sandbox
> user = User.new(name: “Example User”, email: “user@example.com”,password: “foobar”, password_confirmation: “foobar”)
> user.save()
> user.save(:validate => false)
> user.valid?
> user.errors.inspect
> puts user

Rebuild db
$ bundle exec rake db:drop
$ bundle exec rake db:create
$ bundle exec rake db:migrate
$ bundle exec rake db:test:prepare
$ bundle exec rspec spec/

$ bundle exec rake db:reset
$ bundle exec rake db:populate
$ bundle exec rake db:test:prepare

iosbyexample
$ bundle exec rake db:all
$ bundle exec rake db:test:prepare
$ bundle exec rspec spec

Note: if you haven’t run unit tests in a while do a test:prepare

Misc

$ rake routes
$ rspec .

it “foo” do
puts page.body
end

– controller
logger.debug “===================”

RVM

https://agilewarrior.wordpress.com/2014/10/11/rvm-management-workflow/
$ rvm –help
$ rvm gemset –help
$ rvm gemset list
$ rvm gemset use rails310rc4
$ rvm 1.8.7-p302 (switch to this version of ruby)
$ rvm gemset delete rails2tutorial
http://rvm.io/rvm/basics

How to setup a name gemset

$ rvm use 1.9.2
$ rvm gemset create rails310
$ rvm 1.9.2@rails310
$ gem install rails

switch versions:

$ rvm 1.9.2@rails126 ; rails –version
$ rvm 1.9.2@rails310 ; rails –version

$ rvm –default use ruby-1.9.3-p125

$ rvm –help

$ rvm use 2.0

managing old gem versions
$ gem list
$ gem cleanup rjb
$ gem uninstall rjb
$ gem uninstall rjb –version 1.1.9

$ gem install rspec -v 2.14.1
$ gem uninstall rspec

Testing

it "foo" do
        puts page.body
end

Diff between <%= and <=

 <% %> is a snippet of Rails code (ie starting a conditional, ending a conditional, etc)
 <%= %> actually evaluates an expression and returns a value to the page

<%= s.trade_date %> - output the trade date to screen
<% if m.is_cdn_pwr? %> - evaluate this conditional (output nothing)

Links that help

RVM Gemset Management
Hashes & Symbols
Rails Generator Cheat Sheet
How to create user
how-to-create-static-pages-rails
how-to-model-has-two-associations-in-rails
how-to-setup-rspec-from-scratch
how-to-setup-a-new-ruby-project-with-tests
how-to-create-a-staging-environment-heroku
how-to-create-a-named-app-in-heroku
jquery-links-and-elements-not-reloading-in-rails
how-to-use-glyphicons-in-twitter-bootstrap/
how-to-add-jquery-ui-theme-to-your-rails-app
how-include-jquery-ui-in-your-rails-app/
how-to-setup-twitter-bootstrap-rails-from-scratch-rails
how-to-add-a-flash-message-to-your-rails-page
how-to-pass-variable-to-partials-in-rails
how-to-specify-controller-and-action-in-rails-form
how-to-import-heroku-postgres-locally-from-prod-rails
how-to-install-postgres-rails
how-to-setup-and-amazon-webservices-and-connect-to-from-rails
how-to-create-a-new-rails-page
rails-has_many-example

Rubymine shortcuts
Option up arrow Extend selection in preferences MAC OSX 10.5+ keyboard mapping
Generate Code (tdd new method) – option + Enter
New test – ase -> tab
Refactor this – ^T
Live Templates – Cmd J

http://www.jetbrains.com/ruby/docs/RubyMine_ReferenceCard_Mac.pdf
http://pivotallabs.com/code-generation-in-rubymine/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: