Rails
Application
rails new my_app --database=postgresql # create app
rails s # start
rails s -p 1234 # start with port 1234
rails c # console
rake -T # list task
Scaffold
rails g scaffold product name:string price:decimal{7,2}
Check on Heroku Reserved Words before naming
Migration
rake db:drop
rake db:create
rake db:migrate
rake db:rollback
RAILS_ENV=test rake db:drop
Add column
rails g migration add_email_to_users email:string
Common Migration datatype
:binary
:boolean
:date
:datetime
:decimal
:float
:integer
:bigint
:primary_key
:string
:text
:time
:timestamp
Rspec
rspec
rspec spec/models/user.spec.rb
rspec spec/models/user.spec.rb:43
Spec for ‘belong to’
assc = described_class.reflect_on_association(:user_group)
expect(assc.macro).to eq :belongs_to
Spec for presence of
group = FactoryBot.build(:application_group, name: '')
expect(template).to_not be_valid
Rake Task
Create rake task with argument
task :task1 => [:environment] do
puts ENV['path']
end
task :task2 => [:environment] do
puts ARGV[1]
end
task :task3, [:message] => :environment do |t, args|
args.with_defaults(:path => "default")
puts args.path
end
rake task1 path="your path"
rake task2 "your_path"
rake "task3[your_path]"