ActiveRecord Flashcards

1
Q

Base: Rails creates Ruby ________ files in db/migrate

A

Rails creates Ruby migration files in db/migrate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Base: Subclass _________::Base to create __________

A

Subclass ActiveRecord::Base to create persistence

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Base: rake db:migrate does the _________ migration

A

rake db:migrate does the database migration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Rake –tasks

A

$ rake –tasks

(in /Users/beaty-admin/Documents/DU/4585/demo)

rake about # List versions of all Rails frameworks and the env…

rake assets:clean # Remove compiled assets

rake assets:precompile # Compile all the assets named in config.assets.pre…

rake db:create # Create the database from DATABASE_URL or config/d…

rake db:drop # Drops the database using DATABASE_URL or the curr…

rake db:fixtures:load # Load fixtures into the current environment’s data…

rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE…

rake db:migrate:status # Display status of migrations

rake db:rollback # Rolls the schema back to the previous version (sp…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

rake –tasks Continued

A

rake db:schema:dump # Create a db/schema.rb file that can be portably u…

rake db:schema:load # Load a schema.rb file into the database

rake db:seed # Load the seed data from db/seeds.rb

rake db:setup # Create the database, load the schema, and initial…

rake db:structure:dump # Dump the database structure to db/structure.sql

rake db:version # Retrieves the current schema version number

rake doc:app # Generate docs for the app – also available doc:r…

rake log:clear # Truncates all *.log files in log/ to zero bytes

rake middleware # Prints out your Rack middleware stack

rake notes # Enumerate all annotations (use notes:optimize, :f…

rake notes:custom # Enumerate a custom annotation, specify with ANNOT…

rake rails:template # Applies the template supplied by LOCATION=(/path/…

rake rails:update # Update configs and some other initially generated…

rake routes # Print out all defined routes in match order, with…

rake secret # Generate a cryptographically secure secret key (t…

rake stats # Report code statistics (KLOCs, etc) from the appl…

rake test # Runs test:units, test:functionals, test:integrati…

rake test:recent # Run tests for {:recent=>“test:prepare”} / Test re…

rake test:single # Run tests for {:single=>“test:prepare”}

rake test:uncommitted # Run tests for {:uncommitted=>“test:prepare”} / Te…

rake time:zones:all # Displays all time zones, also available: time:zon…

rake tmp:clear # Clear session, cache, and socket files from tmp/ …

rake tmp:create # Creates tmp directories for sessions, cache, sock…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Generated Additions:

•id

–Default primary ___

A

•id

–Default primary key

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Generated Additions:

  • created_at
  • updated_at
  • xxx_id

–Foreign keys

A
  • created_at
  • updated_at
  • xxx_id

–Foreign keys

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Primary keys:

•Can change

–self.primary_key = “column”

A

•Can change

–self.primary_key = “column”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Primary keys:

•Still use “id” to refer to primary key

A

•Still use “id” to refer to primary key

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Relationships:

  • Associations between ______
  • One to ___
  • One to ____
  • Many to _____
A
  • Associations between tables
  • One to one
  • One to many
  • Many to many
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

One to One:

class Employee < ActiveRecord::Base

has_one :office

end

class Office < ActiveRecord::Base

belongs_to :employee

foreign key - employee_id

end

A

class Employee < ActiveRecord::Base

has_one :office

end

class Office < ActiveRecord::Base

belongs_to :employee

foreign key - employee_id

end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

ERD:

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Rule:
•The model that has the foreign key always has the belongs_to

A

•The model that has the foreign key always has the belongs_to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Generated Methods for belongs_to and has_one:

  • other
  • other=(other)
  • build_other(attributes={})
  • create_other(attributes={})
  • create_other!(attributes={})
A

Generated Methods for belongs_to and has_one:

  • other
  • other=(other)
  • build_other(attributes={})
  • create_other(attributes={})
  • create_other!(attributes={})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

One to Many:

class Manager < ActiveRecord::Base

has_many :employees

end

class Employee < ActiveRecord::Base

belongs_to :manager

foreign key - manager_id

end

A

class Manager < ActiveRecord::Base

has_many :employees

end

class Employee < ActiveRecord::Base

belongs_to :manager

foreign key - manager_id

end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

One to Many ERD:

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

So…

$ rails new advisorandstudent

$ cd advisorandstudent

$ rails generate scaffold Student name:string advisor:references

$ rails generate scaffold Advisor name:string

A

$ rails new advisorandstudent

$ cd advisorandstudent

$ rails generate scaffold Student name:string advisor:references

$ rails generate scaffold Advisor name:string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Modify:

  • app/models/advisor.rb
  • app/views/students/index.html.erb
  • app/views/students/show.html.erb
  • app/views/advisors/show.html.erb
A
  • app/models/advisor.rb
  • app/views/students/index.html.erb
  • app/views/students/show.html.erb
  • app/views/advisors/show.html.erb
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Modify Advisor Model:

$ more app/models/advisor.rb

class Advisor < ActiveRecord::Base

has_many :students

end

A

$ more app/models/advisor.rb

class Advisor < ActiveRecord::Base

has_many :students

end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Modify Student Model (Maybe):

$ more app/models/student.rb

class Student < ActiveRecord::Base

belongs_to :advisor

end

A

$ more app/models/student.rb

class Student < ActiveRecord::Base

belongs_to :advisor

end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

app/views/students/_form.html.erb:

[…]

<%= f.label :advisor_id %>

<%= f.select(:advisor_id,

options_from_collection_for_select(Advisor.all,

:id, :name)) %>

[…]

A

[…]

<%= f.label :advisor_id %>

<%= f.select(:advisor_id,

options_from_collection_for_select(Advisor.all,

:id, :name)) %>

[…]

23
Q

app/views/students/index.html.erb:

A
24
Q

app/views/advisors/show.html.erb:

A
25
Q

Many to Many:

class Assignment < ActiveRecord::Base

belongs_to :programmer # foreign key - programmer_id

belongs_to :project # foreign key - project_id

end

class Programmer < ActiveRecord::Base

has_many :assignments

has_many :projects, through: :assignments

end

class Project < ActiveRecord::Base

has_many :assignments

has_many :programmers, through: :assignments

end

A

class Assignment < ActiveRecord::Base

belongs_to :programmer # foreign key - programmer_id

belongs_to :project # foreign key - project_id

end

class Programmer < ActiveRecord::Base

has_many :assignments

has_many :projects, through: :assignments

end

class Project < ActiveRecord::Base

has_many :assignments

has_many :programmers, through: :assignments

end

26
Q

Many to Many ERD:

A
27
Q

Join:

  • Assignment model a join or junction table
  • You have to create explicitly
A
  • Assignment model a join or junction table
  • You have to create explicitly
28
Q

HABTM:

class Programmer < ActiveRecord::Base

has_and_belongs_to_many :projects

end

class Project < ActiveRecord::Base

has_and_belongs_to_many :programmers

end

A

class Programmer < ActiveRecord::Base

has_and_belongs_to_many :projects

end

class Project < ActiveRecord::Base

has_and_belongs_to_many :programmers

end

29
Q

Join Table Model:

  • You still have to create table
  • Name must be named programmers_projects

–Lexical (alphabetic) order

•Before

$ rails generate model ProgrammersProjects programmer_id:integer project_id:integer

•Now:

$ rails generate model ProgrammersProjects programmer:references project:references

A
  • You still have to create table
  • Name must be named programmers_projects

–Lexical (alphabetic) order

•Before

$ rails generate model ProgrammersProjects programmer_id:integer project_id:integer

•Now:

$ rails generate model ProgrammersProjects programmer:references project:references

30
Q

Basically:

  • Create two models and a join table
  • Update two models with HABTM
  • Use select options similarly as before
  • Iterate in show view using foreign key
A
  • Create two models and a join table
  • Update two models with HABTM
  • Use select options similarly as before
  • Iterate in show view using foreign key
31
Q

Basically Continued:

  • Add foreign key to permit in controller
  • That’s it

–Conventions take care of all the insertions into the join table

A
  • Add foreign key to permit in controller
  • That’s it

–Conventions take care of all the insertions into the join table

32
Q

Create:

$ rails new bookslibraries

$ cd bookslibraries

$ rails generate scaffold Book name:string

$ rails generate scaffold Library name:string

$ rails generate model BooksLibraries book:references library:references

$ rake db:migrate

A

$ rails new bookslibraries

$ cd bookslibraries

$ rails generate scaffold Book name:string

$ rails generate scaffold Library name:string

$ rails generate model BooksLibraries book:references library:references

$ rake db:migrate

33
Q

app/controllers/books_controller.rb

— a/app/controllers/books_controller.rb

+++ b/app/controllers/books_controller.rb

@@ -15,10 +15,12 @@ class BooksController < ApplicationController

GET /books/new

def new

@book = Book.new

+ @libraries = Library.all

end

GET /books/1/edit

def edit

+ @libraries = Library.all

end

POST /books

@@ -69,6 +71,6 @@ class BooksController < ApplicationController

Never trust parameters from the scary internet, only allow the white list through.

def book_params

  • params.require(:book).permit(:name)

+ params.require(:book).permit(:name, :library_ids => [])

end

end

A

— a/app/controllers/books_controller.rb

+++ b/app/controllers/books_controller.rb

@@ -15,10 +15,12 @@ class BooksController < ApplicationController

GET /books/new

def new

@book = Book.new

+ @libraries = Library.all

end

GET /books/1/edit

def edit

+ @libraries = Library.all

end

POST /books

@@ -69,6 +71,6 @@ class BooksController < ApplicationController

Never trust parameters from the scary internet, only allow the white list through.

def book_params

  • params.require(:book).permit(:name)

+ params.require(:book).permit(:name, :library_ids => [])

end

end

34
Q

app/controllers/libraries_controller.rb

— a/app/controllers/libraries_controller.rb

+++ b/app/controllers/libraries_controller.rb

@@ -15,10 +15,12 @@ class LibrariesController < ApplicationController

GET /libraries/new

def new

@library = Library.new

+ @books = Book.all

end

GET /libraries/1/edit

def edit

+ @books = Book.all

end

POST /libraries

@@ -69,6 +71,6 @@ class LibrariesController < ApplicationController

Never trust parameters from the scary internet, only allow the white list through.

def library_params

  • params.require(:library).permit(:name)

+ params.require(:library).permit(:name, :book_ids => [])

end

end

A

— a/app/controllers/libraries_controller.rb

+++ b/app/controllers/libraries_controller.rb

@@ -15,10 +15,12 @@ class LibrariesController < ApplicationController

GET /libraries/new

def new

@library = Library.new

+ @books = Book.all

end

GET /libraries/1/edit

def edit

+ @books = Book.all

end

POST /libraries

@@ -69,6 +71,6 @@ class LibrariesController < ApplicationController

Never trust parameters from the scary internet, only allow the white list through.

def library_params

  • params.require(:library).permit(:name)

+ params.require(:library).permit(:name, :book_ids => [])

end

end

35
Q

app/models/book.rb and app/models/library.rb:

— a/app/models/book.rb

+++ b/app/models/book.rb

@@ -1,2 +1,3 @@

class Book < ActiveRecord::Base

+ has_and_belongs_to_many :libraries

end

— a/app/models/library.rb

+++ b/app/models/library.rb

@@ -1,2 +1,3 @@

class Library < ActiveRecord::Base

+ has_and_belongs_to_many :books

end

A

— a/app/models/book.rb

+++ b/app/models/book.rb

@@ -1,2 +1,3 @@

class Book < ActiveRecord::Base

+ has_and_belongs_to_many :libraries

end

— a/app/models/library.rb

+++ b/app/models/library.rb

@@ -1,2 +1,3 @@

class Library < ActiveRecord::Base

+ has_and_belongs_to_many :books

end

36
Q

app/views/books/_form.html.erb:

— a/app/views/books/_form.html.erb

+++ b/app/views/books/_form.html.erb

@@ -15,6 +15,11 @@

<%= f.label :name %>

<%= f.text_field :name %>

+

+ <%= f.label :library_id %>

+ <%= f.collection_check_boxes(:library_ids, @libraries, :id, :name) %>

+

+

<%= f.submit %>

A

— a/app/views/books/_form.html.erb

+++ b/app/views/books/_form.html.erb

@@ -15,6 +15,11 @@

<%= f.label :name %>

<%= f.text_field :name %>

+

+ <%= f.label :library_id %>

+ <%= f.collection_check_boxes(:library_ids, @libraries, :id, :name) %>

+

+

<%= f.submit %>

37
Q
A
38
Q

app/views/books/_form.html.erb:

— a/app/views/books/_form.html.erb

+++ b/app/views/books/_form.html.erb

@@ -15,6 +15,11 @@

<%= f.label :name %>

<%= f.text_field :name %>

+

+ <%= f.label :library_id %>

+ <%= f.collection_check_boxes(:library_ids, @libraries, :id, :name) %>

+

+

<%= f.submit %>

A

— a/app/views/books/_form.html.erb

+++ b/app/views/books/_form.html.erb

@@ -15,6 +15,11 @@

<%= f.label :name %>

<%= f.text_field :name %>

+

+ <%= f.label :library_id %>

+ <%= f.collection_check_boxes(:library_ids, @libraries, :id, :name) %>

+

+

<%= f.submit %>

39
Q

app/views/books/show.html.erb:

— a/app/views/books/show.html.erb

+++ b/app/views/books/show.html.erb

@@ -5,5 +5,11 @@

<%= @book.name %>

+

+ <% @book.libraries.each do |library| %>

+

  • <%= library.name %>

+ <% end %>

+

+

<%= link_to ‘Edit’, edit_book_path(@book) %> |

<%= link_to ‘Back’, books_path %>

A

— a/app/views/books/show.html.erb

+++ b/app/views/books/show.html.erb

@@ -5,5 +5,11 @@

<%= @book.name %>

+

+ <% @book.libraries.each do |library| %>

+

  • <%= library.name %>

+ <% end %>

+

+

<%= link_to ‘Edit’, edit_book_path(@book) %> |

<%= link_to ‘Back’, books_path %>

40
Q

app/views/libraries/_form.html.erb

A
41
Q

app/views/libraries/show.html.erb

A
42
Q

Accessing Data:

•.find(id)

–.find(id,id,…)

•Dynamic finder

–Student.find_by_name (“Steve Beaty”)

–Student.find_last_by_name (“Steve Beaty”)

–Student.find_all_by_name (“Steve Beaty”)

A

•.find(id)

–.find(id,id,…)

•Dynamic finder

–Student.find_by_name (“Steve Beaty”)

–Student.find_last_by_name (“Steve Beaty”)

–Student.find_all_by_name (“Steve Beaty”)

43
Q

More Dynamic:

  • Student.find_by_name_and_email(“Steve Beaty”, “steven.beaty@du.edu”)
  • Student.find_by_name!(“Steve Beaty”)
  • Raises exception if not found
  • Instead of returning nil
  • find_or_initialize_by_()
  • find_or_create_by_()
A
  • Student.find_by_name_and_email(“Steve Beaty”, “steven.beaty@du.edu”)
  • Student.find_by_name!(“Steve Beaty”)
  • Raises exception if not found
  • Instead of returning nil
  • find_or_initialize_by_()
  • find_or_create_by_()
44
Q

Embedding SQL:

•Students.where([“name = ? and major = ‘cs’”, name])

–Cleans up SQL to avoid injections

•Where takes a hash

–Student.where(params[:order])

A

•Students.where([“name = ? and major = ‘cs’”, name])

–Cleans up SQL to avoid injections

•Where takes a hash

–Student.where(params[:order])

45
Q

Like, SQL:

  • Student.where(“name like ?”, “Steve%”)
  • Student.where(name: ‘Steve’).order(“GPA”)
  • Student.where(name: ‘Steve’).limit(10)
  • Student.where(name: ‘Steve’).select(“name, GPA”)
  • Can also specify joins
A
  • Student.where(“name like ?”, “Steve%”)
  • Student.where(name: ‘Steve’).order(“GPA”)
  • Student.where(name: ‘Steve’).limit(10)
  • Student.where(name: ‘Steve’).select(“name, GPA”)
  • Can also specify joins
46
Q

Column Operations:

•Student.average(:GPA)

–.maximum, .minimum, .sum, .count

•Student.group(:state).average(:GPA)

–Average GPA per state

A

•Student.average(:GPA)

–.maximum, .minimum, .sum, .count

•Student.group(:state).average(:GPA)

–Average GPA per state

47
Q

Scopes:

•Create a scope of “where”s to operate on

class Student

scope :deans_list, lambda {|gpa| where(‘GPA > 3.5’, gpa)}

end

A

•Create a scope of “where”s to operate on

class Student

scope :deans_list, lambda {|gpa| where(‘GPA > 3.5’, gpa)}

end

48
Q

Bang!

  • .save returns true if record saved, nil otherwise
  • .save! raises exception if not saved
  • .create always return ActiveRecord object, saved or not
  • .create! raises exception
A
  • .save returns true if record saved, nil otherwise
  • .save! raises exception if not saved
  • .create always return ActiveRecord object, saved or not
  • .create! raises exception
49
Q

Removing Data:

  • .delete
  • .destroy

–Does a delete and then freezes object

–Use destroy in most cases

A
  • .delete
  • .destroy

–Does a delete and then freezes object

–Use destroy in most cases

50
Q

Callbacks:

  • One can inject code at various stages of save and destroy (but not delete)
  • Can help keep model data clean
A
  • One can inject code at various stages of save and destroy (but not delete)
  • Can help keep model data clean
51
Q

Callbacks Continued:

•Before and after

–Validation

–Save

–Create

–Update

–Destroy

•Also after_find and after_initialize

A

•Before and after

–Validation

–Save

–Create

–Update

–Destroy

•Also after_find and after_initialize

52
Q

Validation:

class CreditCard < ActiveRecord::Base

Strip everything but digits, so the user can specify “555 234 34” or

“5552-3434” and both will mean “55523434”

before_validation(on: :create) do

self.number = number.gsub(/[^0-9]/, “”) if attribute_present?(“number”)

end

end

A

class CreditCard < ActiveRecord::Base

Strip everything but digits, so the user can specify “555 234 34” or

“5552-3434” and both will mean “55523434”

before_validation(on: :create) do

self.number = number.gsub(/[^0-9]/, “”) if attribute_present?(“number”)

end

end

53
Q

Private Method Callback:

class Subscription < ActiveRecord::Base

before_create :record_signup

private

def record_signup

self.signed_up_on = Date.today

end

end

A

class Subscription < ActiveRecord::Base

before_create :record_signup

private

def record_signup

self.signed_up_on = Date.today

end

end

54
Q

Doco:

  • http://api.rubyonrails.org/
  • rdoc

$ cd /usr/local/rvm/gems/ruby-1.9.3-p392/gems

$ rdoc

$ open doc/index.html

A
  • http://api.rubyonrails.org/
  • rdoc

$ cd /usr/local/rvm/gems/ruby-1.9.3-p392/gems

$ rdoc

$ open doc/index.html