Skip to content
Browse files

Here are some helpful notes on tomorrows class. The first is an overv…

…iew of ActiveRecord, these are more just notes I took and am sharing with you guys.

The notes on MySQL and potential hiccups when getting it to work with Rails 3, based on dependiences issues, or possible misconfiguration.

Lastly, there are notes on tomorrows class, what we are going through and what we will learn.
  • Loading branch information...
1 parent 6d6afc3 commit acbf57206bbb53e2f76d53dfd79d251e006b16e5 @newfront committed Nov 13, 2010
Showing with 598 additions and 0 deletions.
  1. +393 −0 week_one/ActiveRecord_notes.rtf
  2. +20 −0 week_one/MySQL_help.rtf
  3. +185 −0 week_one/Overview.rtf
View
393 week_one/ActiveRecord_notes.rtf
@@ -0,0 +1,393 @@
+{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
+{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
+{\colortbl;\red255\green255\blue255;}
+\margl1440\margr1440\vieww12100\viewh10160\viewkind0
+\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural
+
+\f0\b\fs24 \cf0 MySQL\
+(
+\b0 if you want to use mysql2 gem)\
+mysql2 gem\
+sudo /usr/local/ruby19/bin/gem install mysql2\
+\
+Person (model)\
+people_controller (controller)\
+\
+person_id\
+\
+>> rails console (open up the rails integrated console)\
+\
+
+\b >> Person
+\b0 \
+(Shows everything you need to know about the db Model)\
+\
+=> #<Person id: nil, name: nil, age: nil, description: nil, priority: nil, log_date: nil, created_at: nil, updated_at: nil>\
+\
+>>
+\b p = Person.new
+\b0 \
+=> Creates new Person Object\
+\
+
+\b >> p.class
+\b0 \
+\
+=> Person(id: integer, name: string, age: integer, description: text, priority: integer, log_date: datetime, created_at: datetime, updated_at: datetime)\
+\
+
+\b >> p.is_a? Person
+\b0 \
+=> true\
+\
+
+\b Creating Records\
+
+\b0 (Based off of the Person model)\
+\
+>> p = Person.new\
+\
+>> p.new_record?\
+=> true\
+\
+>> p.name = "Scott"\
+>> p.age = 26\
+>> p.description = "I love to write code with Ruby on Rails"\
+>> p.priority = 1\
+>> p.log_date = Time.now.to_s\
+>> p.save\
+=> true\
+\
+
+\b >> p.new_record?
+\b0 \
+=> false\
+\
+Alternatively, you can create the new Object all in one fell swoop. \
+\
+>> Person.create(:name => "Scott Haines", :age => 26, :priority => 3, :log_date => Time.now.to_s, :description => "I love to program")\
+\
+Above we create a new Person Object, add values for the core Object variables, and then use the
+\b save
+\b0 method to save to our database.\
+\
+
+\b Looking up Records
+\b0 \
+\
+>> Person.find(:all)\
+=> \
+\
+>> Person.find.first\
+>> Person.find.last\
+\
+>> Person.find_by_name("Scott Haines")\
+\
+>> Person.find(:first, :conditions => \{:name => "Scott Haines")\
+\
+>> Person.where(:name => "Scott Haines")\
+\
+>> Person.find(:all).each \{|people| puts "Found new object #\{people.inspect\}"\}\
+\
+
+\b Updating Records\
+\
+
+\b0 >> p = Person.first
+\b \
+
+\b0 >> p.update_attributes(:name => "Scott Haines2")\
+=> true\
+\
+>> p = Person.first\
+>> p.name = "Scott"\
+>> p.save\
+=> true\
+\
+
+\b Deleting Records\
+
+\b0 \
+>> Person.all.count => 2\
+>> p = Person.first\
+>> p.destroy\
+\
+=> outputs the Object values that have been removed\
+\
+>> Person.all.count => 1\
+\
+>> Person.first.delete\
+\
+
+\b Recovering Errors\
+\
+
+\b0 >> p.errors.any?\
+\
+\
+Process\
+(Assuming your in command line at app root)\
+1. Create Blog\
+>> rails generate model Article title:string published_at:datetime body:text\
+>> rake db:migrate\
+\
+(Note: rake db:schema:load will update your MySQL schema)\
+\
+2. Check your work\
+\
+>> rails console\
+\
+(try to create a new Article)\
+\
+>> a = Article.new\
+=> #<Article id: nil, title: nil, published_at: nil, body: nil, created_at: nil, updated_at: nil>\
+>> a.title = "Brushing off Cat Hair"\
+=> "Brushing off Cat Hair"\
+>> a.published_at = Time.now.to_s\
+=> "Fri Nov 12 21:01:14 -0800 2010"\
+>> a.save\
+=> false\
+>> a.body = "Me Me Me"\
+=> "Me Me Me"\
+>> a.save\
+=> true\
+>> a.errors.any?\
+=> false\
+\
+You notice that you can only
+\b save
+\b0 once you have passed your validations. \
+\
+If you update anything in your Model's source, simply reload the console environment with the following command\
+\
+>> reload!\
+\
+
+\b Checking your Errors\
+
+\b0 >> a = Article.new\
+=> #<Article id: nil, title: nil, published_at: nil, body: nil, created_at: nil, updated_at: nil>\
+>> a.save\
+=> false\
+>> a.errors.any?\
+=> true\
+>> a.errors\
+=> #<OrderedHash \{:title=>["can't be blank"], :body=>["can't be blank"]\}>\
+\
+Now you can use these errors in your code so it is easier for people to know what they did wrong, etc. \
+\
+3. Create a User class\
+>> rails generate model User email:string password:string\
+\
+>> rake db:migrate\
+\
+4. Create a Profile class\
+>> rails generate model Profile user_id:integer name:string birthday:date bio:text color:string twitter:string\
+\
+>> rake db:migrate\
+\
+5. Test in the console\
+\
+>> rails console\
+>> reload!\
+\
+>> User.create(:email => "scott@newfrontcreative.com", :password => "toughL0ve")>> profile = Profile.create(:user_id => 1, :name => "Scott Haines", :birthday => Time.local(1984, 3, 16), :bio => "I am not good at hunting or trapping, but I am good at programming", :color => "red", :twitter => "newfront")\
+\
+
+\b Making connections (working with ActiveRecord associations)\
+\
+
+\b0 User has_one :profile\
+Profile belongs_to :user\
+\
+>> user = User.first\
+>> profile = Profile.first\
+\
+>> user.profile = profile\
+>> user.save\
+=> true\
+\
+Now we can reference a User's profile from the main User Object. \
+\
+>> user\
+=> #<User id: 1, email: "scott@newfrontcreative.com", password: "toughL0ve", created_at: "2010-11-13 05:14:18", updated_at: "2010-11-13 05:14:18">\
+\
+>> user.profile\
+=> #<Profile id: 1, user_id: 1, name: "Scott Haines", birthday: "1984-03-16", bio: "I am not good at hunting or trapping, but I am good...", color: "red", twitter: "newfront", created_at: "2010-11-13 05:22:17", updated_at: "2010-11-13 05:22:17">\
+\
+So this is good for making "relations" in your relational database. Takes the effort out of working with large data sets. \
+\
+>> user.create_profile :name => "Scott Haines", :color => "green"\
+\
+Above is another way of working with associations. \
+\
+(Link people to Articles)\
+6. >> rails generate migration add_user_id_to_articles user_id:integer\
+>> rake db:migrate\
+\
+7. Test that everything works\
+>> rails console\
+\
+>> user = User.first\
+\
+>> article = Article.create(\'85)\
+\
+>> user.articles\
+\
+(notice that the article is automatically mapped to the user as long as the user_id is the same as the User id.)\
+\
+*Fancy Trick\
+\
+>> user.articles << Article.first\
+\
+We use the leftshift (<<) operator to append the Object to our User object.\
+\
+>> user.articles.first\
+>> user.articles.last\
+\
+>> user.articles.find(:first)\
+\
+You can search the association like you would a simple ActiveRecord object\
+\
+
+\b Creating ActiveRecord Lookup Tables\
+
+\b0 \
+has_and_belongs_to_many\
+\
+8. Create a Category Model\
+>> rails generate model Category name:string\
+\
+Link Article to Category\
+\
+>> rails generate migration create_articles_categories\
+\
+(This creates an empty migration)\
+\
+>> rake db:migrate\
+\
+(pops. you have now updated your migrations to latest, but never wrote the new table)\
+\
+open up your migrations (/db/migrate/<date>_create_articles_categories.rb)\
+add create_table method, no foreign key\
+\
+9. Seeding Data\
+(/db/seed.rb)\
+\
+- Add Objects that you want to fill your Database with initially\
+(*Note: Seed values will be populated in the database as many times as you call them, so seed your database once and leave it)\
+\
+rake db:setup (will recreate your database from scratch, then you can migrate and seed your database)\
+\
+10. \
+>> article = Article.first\
+>> article.categories << Category.first\
+\
+Now we have a category associated with an Article which is also associated with a User who has a profile\
+\
+>> article.user\
+=> returns the User object associated with it\
+\
+>> user = User.first\
+>> user.article\
+>> user.profile\
+>> user.articles.first.categories\
+\
+Associations in Ruby on Rails are powerful and simple to get, with a little practice\
+\
+11. Adding Comments\
+\
+>> rails generate model comment article_id:integer name:string body:text\
+>> rake db:migrate\
+\
+Now an article can have a comment, a user can view replies through their articles (comments), etc\
+\
+
+\b Advanced Searching\
+
+\b0 \
+>> Article.where("created_at > '23-03-2010' OR body NOT LIKE '%model%'")\
+\
+This says "find all Articles that were created after March 23rd, 2010 and whose body text does not contain the word
+\b model
+\b0 . The modulo (%) that wraps the word means, find a partial match, or full match. \
+\
+
+\b Add variables into your Search\
+
+\b0 >> Article.where("published_at < ?", Time.now.to_s)\
+\
+>> Article.where
+\b ("title LIKE :search OR body LIKE :search",\{:search => '%Dog%'\})
+\b0 \
+=> [#<Article id: 1, title: "The Lazy Dog", published_at: "2010-11-13 06:11:45", body: "Jumps over the Quick Brown Fox", created_at: "2010-11-13 06:12:11", updated_at: "2010-11-13 06:12:11", user_id: 1>]\
+\
+
+\b View the generated SQL\
+
+\b0 >> Article.where("published_at < ?", Time.now.to_s).to_sql\
+=> "SELECT `articles`.* FROM `articles` WHERE (published_at < 'Sat Nov 13 10:48:50 -0800 2010')"\
+\
+
+\b ActiveRecord Search Methods\
+where
+\b0 (conditions)\
+\
+
+\b order
+\b0 ("string_for_field DESC|ASC)\
+\
+
+\b limit
+\b0 (Limit the returns to a specific number, or range)\
+\
+
+\b joins
+\b0 (joined as an SQL Fragment)\
+\
+
+\b includes
+\b0 (joins associated tables into ActiveRecord Object)\
+\
+\
+Working with ActiveRecord and Rails (applying
+\b scope
+\b0 )\
+\
+(Applied to article.rb)\
+\
+scope :published, where("articles.published_at IS NOT NULL")\
+scope :draft, where("articles.published_at IS NULL")\
+\
+# Remember that the lambda is a Ruby method for creating a method declaration on the fly\
+scope :recent, lambda \{ published.where("articles.published_at > ?", 1.week.ago.to_date)\}\
+\
+# Add a custom search method into the Object\
+scope :where_title, lambda \{|term| where("articles.title LIKE ?", "%#\{term\}%")\}\
+\
+# Example Usage: >> Article.published.where_title("Advanced Active Record")\
+\
+\
+
+\b Working with Validations
+\b0 \
+\
+validates :email, :uniqueness => true\
+\
+>> User\
+=> User(id: integer, email: string, password: string, created_at: datetime, updated_at: datetime)\
+>> User.create(:email => "scott@newfrontcreative.com", :password => "joeuser")\
+=> #<User id: 3, email: "scott@newfrontcreative.com", password: "joeuser", created_at: "2010-11-13 19:04:59", updated_at: "2010-11-13 19:04:59">\
+>> User.create(:email => "scott@newfrontcreative.com", :password => "joeuser")\
+=> #<User id: nil, email: "scott@newfrontcreative.com", password: "joeuser", created_at: nil, updated_at: nil>\
+>> u = User.create(:email => "scott@newfrontcreative.com", :password => "joeuser")\
+=> #<User id: nil, email: "scott@newfrontcreative.com", password: "joeuser", created_at: nil, updated_at: nil>\
+>> u.errors\
+=> #<OrderedHash \{:email=>["has already been taken"]\}>\
+\
+validates :email, :uniqueness => true, :length => \{:within => 5..50\}\
+\
+\
+\
+\
+}
View
20 week_one/MySQL_help.rtf
@@ -0,0 +1,20 @@
+{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf320
+{\fonttbl\f0\fmodern\fcharset0 Courier;\f1\fmodern\fcharset0 Courier-Bold;}
+{\colortbl;\red255\green255\blue255;\red246\green246\blue246;\red0\green109\blue109;\red220\green0\blue50;
+}
+\margl1440\margr1440\vieww20900\viewh10980\viewkind0
+\deftab720
+\pard\pardeftab720\sl280\ql\qnatural
+
+\f0\fs24 \cf0 \cb2 sudo env \cf3 ARCHFLAGS
+\f1\b \cf0 =
+\f0\b0 \cf4 "-arch x86_64"\cf0 /usr/local/ruby19/bin/gem install mysql -- --with-mysql-config
+\f1\b =
+\f0\b0 /usr/local/mysql/bin/mysql_config\
+\
+MySQL Issues\
+\
+\pard\pardeftab720\ql\qnatural
+\cf0 \cb2 rake db:setup --trace (view what went wrong)\
+\
+If you find that your having issues connecting to MySQL through Rails, it is probably an issue with "Rails" expectations, by default the database.yml file will attempt to connect to mysql via the /tmp/mysqld.sock, however depending on how you installed mysql, this location might be /var/run/mysqld/mysqld.sock. When in doubt located your mysql.conf file and lookup the socket location.}
View
185 week_one/Overview.rtf
@@ -0,0 +1,185 @@
+{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
+{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
+{\colortbl;\red255\green255\blue255;}
+\margl1440\margr1440\vieww16760\viewh11760\viewkind0
+\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural
+
+\f0\fs24 \cf0 1. Writing your first Rails Application (in one line of code).\
+\
+>> rails new dinner_scaffold\
+>> sudo rails generate scaffold Dinner name:string description:text planned_for:datetime notes:text\
+>> rake db:migrate\
+>> rails server\
+\
+http://localhost:3000/dinners/\
+http://localhost:3000/dinners/1 will show the first record if you have created one\
+\
+2. What makes scaffolding work, what steps are involved in actual rails setup (manual) \
+ a. overview of the rails command line (rails generate <command>)\
+\
+ b. dissection of the rails framework (application folder structure)\
+\
+ /app\
+ /controllers\
+ /models\
+ /helpers\
+ /mailers\
+ /views\
+\
+ /config\
+\
+ /db\
+ /migrate\
+\
+ /public\
+ /javascripts\
+\
+\
+ c.
+\b REST and CRUD
+\b0 \
+ Rest.\
+ Representational State Transfer (REST). REST is a principle that is based on working with information in the form of a resource. Each of these resources has a specific interaction point which is normally represented using a URL. http://domain.com/users/deeplinking Would be a REST URL to perform a deeplinking action on the User(s) of a particular site. \
+\
+ REST is broken up into its HTTP protocol representations. \
+\
+ POST, GET, PUT, DELETE\
+\
+ These HTTP protocol methods have adopted the acronym "CRUD"\
+\
+ Create, Read, Update, Destroy\
+\
+ d.
+\b What is a Route? How does the Rails Request Cycle Work?
+\b0 \
+ Routes in Rails are the URL representation of your Rails Controllers. Each Rails Controller is no more than a Ruby Class and each action (either a view state of a background process) is represented by specific Class Methods. \
+\
+ class UsersController < ApplicationController\
+ def index\
+ end\
+ end\
+\
+ This class would then be made available through the Rails system in the routes.\
+\
+ route get "users/index"\
+\
+ http://localhost:3000/users/index\
+\
+ would call UsersController::index, and would also look for a View in /app/views/users/index.html.erb\
+\
+ 1. Open /config/routes.rb\
+\
+ 2. Create your application "root" path\
+ root :to => "users#index"\
+\
+ 3. Delete index.html from /public\
+\
+ 4. rails server\
+ \
+ 5. http://localhost:3000/ (should now be pointed to UsersController::index and displaying the content of /app/views/users/index.html.erb\
+\
+
+\b Routing in Rails
+\b0 \
+ Routing in Rails has completely changed from version 2.3+ Rails to 3.0. If you are used to the old system of Routing your in for a whole redesign.\
+\
+ Breaking down a Route\
+\
+
+\b route
+\b0 get "users/index" \
+ \
+ /users/index is now available as a get requested resource.\
+\
+ ---------------------------------------------------\
+
+\b Matched Routes (pseudo named routes)
+\b0 \
+\
+ match '/users' => 'users#index' \
+\
+ ---------------------------------------------------\
+\
+
+\b Named Routes\
+
+\b0 \
+ match '/rails/routes/just/work' => "users#index\
+\
+ ---------------------------------------------------------------\
+\
+
+\b http://localhost:3000/users
+\b0 is now the same as
+\b http://localhost:3000/users/index
+\b0 or
+\b http://localhost:3000
+\b0 since we have updated the routing to look for
+\b root
+\b0 at the users index action, and
+\b get
+\b0 "users/index" goes to the same place as
+\b map
+\b0 '/users' => "users#index and lastly
+\b http://localhost:3000/rails/routes/just/work
+\b0 also ends up at UserController::index. \
+\
+\
+ e.
+\b Working with only the Controllers and Views - with and without the command line\
+\
+
+\b0 It is important to remember that you do not have to use the command line for everything, but it typically makes far less mistakes than we do. To build a Controller and give it some actions, we use the following line in our command line. \
+\
+ >> rails generate Controller articles index list show update destroy edit\
+ \
+ >> /usr/local/ruby19/bin/rails generate controller articles index list show update destroy edit\
+ create app/controllers/articles_controller.rb\
+ route get "articles/edit"\
+ route get "articles/destroy"\
+ route get "articles/update"\
+ route get "articles/show"\
+ route get "articles/list"\
+ route get "articles/index"\
+ invoke erb\
+ create app/views/articles\
+ create app/views/articles/index.html.erb\
+ create app/views/articles/list.html.erb\
+ create app/views/articles/show.html.erb\
+ create app/views/articles/update.html.erb\
+ create app/views/articles/destroy.html.erb\
+ create app/views/articles/edit.html.erb\
+ invoke test_unit\
+ create test/functional/articles_controller_test.rb\
+ invoke helper\
+ create app/helpers/articles_helper.rb\
+ invoke test_unit\
+ create test/unit/helpers/articles_helper_test.rb\
+ \
+ So one little line of code generates everything we need from the
+\b routes
+\b0 we need to view the different pages within our website, to the generating a nice little
+\b helper
+\b0 method (we will look at more later), to even creating
+\b tests
+\b0 and
+\b views
+\b0 for our application.\
+ \
+ f. Working with render and redirect_to\
+ a. using Views or rendering directly to the window (:text =>)\
+\
+ g. Working with flash[:] messages\
+ \
+ h. Working with errors\
+ \
+ i. Working with Gemfile, /config/database.yml\
+ \
+ j. Introduction to the Model\
+ rails generate model Person\
+ k. Working with seed.rb (rake db:seed) to prepopulate our data\
+\
+ l. Getting an application up and running and connected to a Datasource.\
+ m. Wrapping Up. What did we learn today. \
+ \
+n. Assignment #1}

0 comments on commit acbf572

Please sign in to comment.
Something went wrong with that request. Please try again.