Gerard Kondner Notes On Ruby Backend
Has_many/Belongs_to
Has_many:/Belongs to : by associating classes with the macros has_many : & belongs_to, active record will go and write a method similar to a filter .all; sequel query that matches results.
How to create .rb files
class Classname < ActiveRecord::Base
blah
end
ActiveRecord::Base needs to be inherited at the beginning of the .rb file
Initialize Method:
Used to customize parameters when creating a new instance
Example:
class Person
def initialize(name)
@name = name
end
end
Ex of a getter and setter method
class Dog
def name
@name
end
def name=(name)
@name = name
end
Self:
What is self? Self refers to the object on which the method is being called.
Self is typically the current active record instance.
Scope is extremely important when using self.
A good example using self:
class CashRegister
attr_accessor :items, :discount, :total, :last_transaction
def initialize(discount = 0)
@total = 0
@discount = discount
@items = []
end
def add_item(title, amount, quantity = 1)
self.last_transaction = amount * quantity
self.total += self.last_transaction
quantity.times do
self.items << title
end
end
def apply_discount
if self.discount != 0
discount_as_percent = (100.0 — self.discount.to_f) / 100
self.total = (self.total * discount_as_percent).to_i
“After the discount, the total comes to $#{self.total}.”
else
“There is no discount to apply.”
end
end
def void_last_transaction
self.total -= self.last_transaction
end
end
Class Methods:
Use the word self when defining
In order to access @@all from a class method, self.all.whatevermethod needs to be used.
A good example of Class methods being used:
class Song
attr_accessor :name, :artist_name
@@all = []
def self.all
@@all
end
def save
self.class.all << self
end
def self.create
song = Song.new
song.save
song
end
def self.new_by_name(song_name)
song = self.new
song.name = song_name
song
end
def self.create_by_name(song_name)
song = self.create
song.name = song_name
song
end
def self.find_by_name(song_name)
self.all.find { |song| song.name == song_name }
end
def self.find_or_create_by_name(song_name)
self.find_by_name(song_name) || self.create_by_name(song_name)
end
def self.alphabetical
self.all.sort_by { |song| song.name }
end
def self.new_from_filename(filename)
parts = filename.split(“ — “)
artist_name = parts[0]
song_name = parts[1].gsub(“.mp3”, “”)
song = self.new
song.name = song_name
song.artist_name = artist_name
song
end
def self.create_from_filename(filename)
song = self.new_from_filename(filename)
song.save
song
end
def self.destroy_all
self.all.clear
end
end
Instance Methods:.
Instance methods are methods that are built to create or display certain information regarding an instance.
A good example of using instance methods is shown below:
class Person
attr_accessor :bank_account
attr_reader :name, :happiness, :hygiene
def initialize(name)
@name = name
@bank_account = 25
@happiness = 8
@hygiene = 8
end
def happiness=(num)
@happiness = if num > 10
10
elsif num < 0
0
else
num
end
end
def hygiene=(num)
@hygiene = if num > 10
10
elsif num < 0
0
else
num
end
end
def clean?
self.hygiene > 7
end
def happy?
self.happiness > 7
end
def get_paid(amount)
self.bank_account += amount
“all about the benjamins”
end
def take_bath
self.hygiene += 4
“♪ Rub-a-dub just relaxing in the tub ♫”
end
def work_out
self.happiness += 2
self.hygiene -= 3
“♪ another one bites the dust ♫”
end
def call_friend(friend)
self.happiness += 3
friend.happiness += 3
“Hi #{friend.name}! It’s #{self.name}. How are you?”
end
def start_conversation(friend, topic)
case topic
when “politics”
[self, friend].each { |person| person.happiness -= 2 }
“blah blah partisan blah lobbyist”
when “weather”
[self, friend].each { |person| person.happiness += 1 }
“blah blah sun blah rain”
else
“blah blah blah blah blah”
end
end
end
General Notes:
· Rake is a gem to be installed
· Make sure to cd into backend/
· When running a Rack Server, command to start server is “shotgun”
· Ex: shotgun — port=9292
· mkdir db/migrate #has to be within db so active record can know what to migrate#
· rake -T #lists all commands#
· write SQL within the db/migrate file, typically:
· create_table :classname do |t|
· Etc. etc.
· Once finished, run rake db:migrate
·
· After db:migrate has run, a development sqlit3 db has been created, as well as the schema!
· Schema is the source of truth for your database, typically DO NOT TOUCH
·
·
· Migrations are tools used by ActiveRecord to design database
· Typical migration syntax is create_ClassName
· The number by create_migration is date&time
· A way to update a migration file AFTER it has already been created, is to create a new migration file; however, rather than using create_table, one should use add_column :migration table name, :column name to add, :value type.
Conclusion
In conclusion, I believed I was able to pick up SQL as well as ActiveRecord relatively easier than understanding how and when to use certain class and instance methods, which is why I made this Practice Page to refer to if I get stuck.