- create new app
rails new moma_app - Sketch out the tables
| Artists | Paintings |
|---|---|
| name | style |
| DoB | title |
| nationality | date |
| bio | artist |
rails generate model Artist
lass CreateArtists < ActiveRecord::Migration
def change
create_table :artists do |t|
t.string :name
t.date :dob
t.string :nationality
t.string :period
t.text :image
t.timestamps
end
end
end
rake db:createrake db:migrate
rails generate model Painting
class CreatePaintings < ActiveRecord::Migration
def change
create_table :paintings do |t|
t.string :title
t.string :year
t.string :medium
t.string :style
t.text :image
t.integer :artist_id
t.timestamps
end
end
end
rake db:migrate
- attr_accessible :has "read" and "write" properties like attr_accessor for rails
-
add attributes to artist.rb and painting.rb, RELATIONSHIPS, and VALIDATIONS
class Painting < ActiveRecord::Base attr_accessible :title, :year, :medium, :style, :image, :artist_id
belongs_to :artist validates_presence_of :year, :artist validates_uniqueness_of :titleend
class Artist < ActiveRecord::Base attr_accessible :name, :nationality, :dob, :period, :image
has_many :paintingsend
-
Add database seeds
Artist.delete_all Painting.delete_all
Artist.create(:name => 'Pablo Picasso', :nationality => 'Spain', :dob => '5/3/1686', :period => 'Impressionistic', :image => 'http://cdn.enjoyourholiday.com/wp-content/uploads/2012/10/Picasso-portrait.jpg') Artist.create(:name => 'Salvador Dali', :nationality => 'Spain', :dob => '4/9/1583', :period => 'Modern', :image => 'http://www.biography.com/imported/images/Biography/Images/Profiles/D/Salvador-Dali-40389-2-402.jpg') Artist.create(:name => 'Claude Monet', :nationality => 'French', :dob => '3/9/1275', :period => 'Post Modern', :image => 'http://www.biography.com/imported/images/Biography/Images/Profiles/M/Claude-Monet-WC-9411771-2-402.jpg') Artist.create(:name => 'Henri Matisse', :nationality => 'French', :dob => '8/3/1376', :period => 'Realists', :image => 'http://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Portrait_of_Henri_Matisse_1933_May_20.jpg/220px-Portrait_of_Henri_Matisse_1933_May_20.jpg') Painting.create(:title => 'Self Portrait', :year => '1245', :medium => 'oil', :style => 'awesome', :image => 'http://www.artcyclopedia.org/art/van-gogh-self.jpg', :artist_id => 1) Painting.create(:title => 'A Woman', :year => '1656', :medium => 'canvas', :style => 'cool', :image => 'http://www.arttherapyblog.com/uimages/2010/10/picasso_woman_b.jpg', :artist_id => 2) Painting.create(:title => 'Butterfly Ship', :year => '1837', :medium => 'water colors', :style => 'quick', :image => 'http://www.idealnetworker.com/home/idealnet/public.html/wp-content/uploads/2011/04/art-salvador-dali.jpg', :artist_id => 3) Painting.create(:title => 'Twilight', :year => '1488', :medium => 'fabric', :style => 'modern', :image => 'http://framingpainting.com/UploadPic/Claude%20Monet/big/Venice%20Twilight.jpg', :artist_id => 4) Painting.create(:title => 'Lydia', :year => '1734', :medium => 'oil', :style => 'other', :image => 'http://www.arthermitage.org/Henri-Matisse/Portrait-of-Lydia-Delectorskaya.jpg', :artist_id => 5)```` -
rake db:seed -
rails consolePulling data from the Active Record First line is the SQL command -
Artist.allselects all information from the artist table -
a = artist.firstSELECT "artists".* FROM "artists" in SQL -
a.paintingslike the JOIN method in SQLSELECT "paintings".* FROM "paintings" WHERE "paintings"."artist_id" = 114.Artist.where(:name => "Pablo Picasso")SELECT "artists".* FROM "artists" WHERE "artists"."name" = 'Pablo Picasso' -
Artist.where(:name => "Pablo Picasso").first.paintingsSELECT "artists".* FROM "artists" WHERE "artists"."name" = 'Pablo Picasso' LIMIT 1SELECT "paintings".* FROM "paintings" WHERE "paintings"."artist_id" = 2 -
Artist.find(1)SELECT "artists".* FROM "artists" WHERE "artists"."id" = ? LIMIT 1 [["id", 1]] -
Artist.find_by_name("Pablo Picasso")SELECT "artists".* FROM "artists" WHERE "artists"."name" = 'Pablo Picasso' LIMIT 1 -
j= Artist.create(name: "Jackie")j.paintings.build(title: "awesome") -
p.errorshave to assign to a variable SO you can call and rails will create the error messsage
-
In order for validation to run, you must try to save to the database
validate :ian def ian if self.year.to_i < Time.now.year errors.add(:year, "Year is in the future") end end
-
Create - NEW and CREATE
-
Read - INDEX and SHOW
-
Update - EDIT and UPDATE
-
Destroy - destroy
-
GET == RECEIVE
-
POST == CREATE
class ArtistsController < ApplicationController
def new #get
@artist = Artist.new
end
def create #post
@artist = Artist.create(params[:id])
render :show
end
def index #get read multiple objects
@artists = Artist.all
end
def show #get read one object
@artist = Artist.find(params[:id])
end
def edit #get
@artist = Artist.find(params[:id])
end
def update #post
artist = Artist.find(params[:id])
artist.update_attributes(params[:artist])
render :show
end
def destroy #deletes
artist = Artist.find(params[:id])
artist.delete
redirect_to(artists_path)
end
end
-
Can use a new one called
-
connects the two html pages
-
What it looks like in the views (SAVE AS _form.html.erb)
redirect_to is a method and the _path you can use to redirect to different route