Skip to content

Instantly share code, notes, and snippets.

@mathildathompson
Created March 19, 2014 04:43
Show Gist options
  • Select an option

  • Save mathildathompson/9635526 to your computer and use it in GitHub Desktop.

Select an option

Save mathildathompson/9635526 to your computer and use it in GitHub Desktop.
rake db:create #connects to psql to connect to the database;
rails g migration create_artists #create an empty migration file;
rails g migration create_artists name:string nationality:string #will write the table for you;
#The filename and he class name in the migrations have to be the same, DONT MESS WITH THEM;
#You do not need to mention ids when you create a table rails will automaticlly do this for you;
rake db:migrate #This will run the most recent migration that it has not run for you; #You can check the schema.rb to check that the migration has been run;
create_table :works do |t| #t is passing the table into the block which we can then use to create the table;
#The name of the model has to be singular (also make sure you name the file singular);
class Artist < ActiveRecord::Base #Active record will go and find the table for us
end
annotate #should tell us one gem has been annotated;
#Go into the rails console and double check that you have a connection;
#In the model add attr_accessible for all the things that you want to be able to mass assign;
resources :artists #The will create all of the CRUD routes;
rails g controller Artists
strftime #string for time
artist.dob.
<%= link_to 'Delete artist', @artist, :method => :delete %>
#The point is that you can use the same URL with different verbs, the idea is that you do not need seprate URLS; Trying to prevent URL proliferation;
#PATCH is a small update;
#PUT is an update;
artist_path(@artist)
@artist => /artist/7
artist
#All three of these will render the artist show action;
edit_artist_path(@artist)
#parital a view that you use within another view;
_form.html.erb #It is convention to use an underscore in front of the partial;
<%= render :partial => 'form' %> #you do not need to use the underscore when you render the partial
<%= render 'form' %> #shorthand for the line above;
#form_for will pre-populate the edit form by using the instance variable you created in the controller;
#Edit artist is being sent as a patch, if you look in the form there is a hidden input with method patch;
An artist has many works and each work belongs to an artist
#We need to add artist_id to the works table;
rails g migration AddArtistIdToWork artist_id:integer
Work.all.first
Work.first #This is the most efficient, it is only going to get one result;
Work.all[0] #This will be the slowest
artist.work << work
work.artist = artist
artist.works.length
artist.works.first.title #Walking the graph;
forger = Artist.new
##RESOURCES
api.rubyonrails.org
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment