Ruby
arbarrington
Posted on September 19, 2022
Gems
Rack - wraps HTTP requests to simplify the Application Programming Interface for servers, frameworks, and software
-Sinatra - Domain Specific Language for creating Ruby web applications, built on top of Rack
-CORS
-Rake
--Seeding
--Migration
-Foreman
gem install foreman
Foreman works with a special kind of file known as a Procfile, which lists different processes to run for our application. Some hosting services, such as Heroku, use a Procfile to run applications, so by using a Procfile in development as well, we'll simplify the deploying process later.
In the root directory, create a file Procfile.dev and add this code:
web: PORT=4000 npm start --prefix client
api: PORT=3000 rails s
Then, run it with Foreman:
foreman start -f Procfile.dev
This will start both React and Rails on separate ports, just like before; but now we can run both with one command!
There is one big caveat to this approach: by running our client and server in the same terminal, it can be more challenging to read through the server logs and debug our code. Furthermore, byebug will not work. If you're doing a lot of debugging in the terminal, you should run the client and server separately to get a cleaner terminal output and allow terminal-based debugging with byebug.
You can run each application separately by opening two terminal windows and running each of these commands in a separate window:
npm start --prefix client
rails s
Active Record
-Associations: One to Many vs Many to Many
--timestamps and the schema
The has_many association reference also lets you provide additional optionsLinks to an external site. to customize its behavior. In our case (and in many cases involving a one-to-many relationship), we can use the dependent: :destroyLinks to an external site. option. This will tell Active Record to delete all the associated records when the parent record is deleted
# foreign_key: true establishes a relationship between a review and a dog house
t.belongs_to :dog_house, null: false, foreign_key: true
has_many :reviews, dependent: :destroy
SQL
Database interaction
Self
"Require" - require relative, require all
"pry" - debugging
"colorize"
Symbols
Classes
Instances
Object Oriented Programming
Benefits and Purposes as a Language
-alternatives
attr_accessor
Class::Class-Constant
Object Inheritance
super
Metaprogramming
t.timestamps => created_at, updated_at
class User
def initialize(attributes)
attributes.each do |key, value|
# create a getter and setter by calling the attr_accessor method
self.class.attr_accessor(key)
self.send("#{key}=", value)
end
end
end
Methods
.order
.pluck
.includes?
.any?
.uniq
.send
.gsub
.detect
.tally
.to_f
.to_i
.split
.strip
.compact
.push
.pop
.shift
.unshift
.each
.sort
.map/.collect
.select
.filter
.filter_by
.max_by
<<
<=>
Posted on September 19, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024