Created
July 29, 2019 10:28
-
-
Save marcoranieri/a20e2c43756e541433037b8441826272 to your computer and use it in GitHub Desktop.
ActiveRecord
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Restaurant < ActiveRecord::Base | |
end | |
# CONVENTIONS over CONFIGURATIONS | |
Table (snake_case) => sport_cars | |
Model (CamelCase) => SportCar | |
Table (snake_case) => products | |
Model (CamelCase) => Product | |
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |
# CREATE | |
hoxton = Restaurant.new(name: "Hoxton 100") # Create an instance | |
hoxton.save # Store in the DATABASE | |
hoxton = Restaurant.create(name: "Hoxton 100") # new + save | |
# UPDATE | |
hoxton.address = "Hoxton" # updating an attribute using WRITER method | |
hoxton.save # Store the new information in the DATABASE | |
# READ | |
restaurants = Restaurant.all # _Array_ of Restaurant Obj | |
second_restaurant = Restaurant.find(2) | |
first_in_hoxton = Restaurant.find_by_address("Hoxton") | |
# All the FIND & FIND_BY Methods return ONE Restaurant Obj | |
# ActiveRecord define for us a FIND_BY method for EACH COLUMN! (find_by_name, find_by_address etc..) | |
# DELETE | |
hoxton.destroy | |
# ADVANCED Queries | |
Restaurant.count | |
london_restaurants = Restaurant.where(address: "London") | |
bars_restauerants = Restaurant.where("name LIKE ?", "%Bar%") | |
recent_restaurants = Restaurant.order(created_at: :desc).limit(3) | |
# WHERE & ORDER return a [ Collection ] of Obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is where you can create initial data for your app. | |
# Restaurant.create(name: "Hoxton 100", address: "Hoxton") | |
# Restaurant.create(name: "Via Emilia", address: "Hoxton") | |
# Restaurant.create(name: "Dinerama", address: "London") | |
require "faker" | |
10.times do |i| | |
Restaurant.create( | |
name: Faker::Name.name , | |
address: Faker::Address.city | |
) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddRatingToRestaurants < ActiveRecord::Migration[5.1] | |
def change | |
add_column :restaurants, :rating, :integer | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateRestaurants < ActiveRecord::Migration[5.1] | |
def change | |
create_table :restaurants do |t| | |
t.string :name | |
t.string :address | |
t.timestamps null: false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment