9. Code generators
• Generate new project
• Generate models
• Generate controllers & views
Saturday, April 13, 13
10. Rake tasks
• Run tests
• Manipulate database
• Show useful info
Saturday, April 13, 13
11. Extensions
• Sensible core extensions
• Helper methods
• A lot of 3rd party gems
Saturday, April 13, 13
12. Convention over
Configuration(1)
• Convention for environments
development, test and production
• Convention for organizing code
• Convention for organizing assets
Saturday, April 13, 13
13. Convention over
Configuration(2)
• Convention for models
model names - table names
model attributes - record fields
• Convention for controllers & views
controller actions - view templates
Saturday, April 13, 13
17. Combine HTTP verb and path to
dispatch requests to corresponding
handlers (actions)
http://guides.rubyonrails.org/routing.html
Saturday, April 13, 13
18. A very simple blog
in 5 minute
Saturday, April 13, 13
23. ActiveRecord can
connect to existing DB
> ActiveRecord::Base.establish_connection(
:adapter => 'mysql2',
:database => 'fju_test_db',
:username => 'root',
:password => '******' )
> ActiveRecord::Base.connection
> ActiveRecord::Base.connected?
Saturday, April 13, 13
24. ActiveRecord can
migrate existing DB
> ARConnection = AR::Base.connection
> ARConnection.create_table(:users) do |t|
t.string :name
t.integer :age
t.timestamps
end
> ARConnection.add_index(:users, 'name')
Saturday, April 13, 13
25. ActiveRecord can
execute raw SQL
> ARConnection = AR::Base.connection
> ARConnection.execute(%q{
INSERT INTO users (name, age)
VALUES ('Mason Chang', 30)
})
> resultes = ARConnection.execute(%q{
SELECT * FROM users WHERE age = 30
})
> results.fields
> results.first
Saturday, April 13, 13
26. User Model
> class User < ActiveRecord::Base
end
> require 'logger'
> ActiveRecord::Base.logger =
Logger.new(STDOUT)
Saturday, April 13, 13
27. Model can create
records
> user.newser = User.new(
:name => 'Eddie Kao', :age => 20)
> user.save
> User.create(
:name => 'Ryudo Teng', :age => 20)
Saturday, April 13, 13
28. Model can retrieve
records
> User.first
> User.last
> User.all
> User.find(1)
> User.find([1,2,3])
> User.find(:first,
:conditions => {:name => 'Eddie Kao'})
> User.find(:all,
:conditions => ['age < ?', 30])
Saturday, April 13, 13
29. Model can update
records
> user = User.find_by_name('Mason Chang')
> user.age = 10
> user.save
> user.update_attribute(:age, 20)
> user.update_attributes(
:name => 'Ming-hsuan Chang'
:age => 30)
> User.update(1, :age => 40)
> User.update_all(
{:age => 50}, ['age >= ?', 20])
Saturday, April 13, 13
30. Model can delete
records
> user = User.first
> user.delete # callbacks ignored
> user.destroy # callbacks triggered
> User.delete(1) # callbacks ignored
> User.destroy(1) # callbacks triggered
Saturday, April 13, 13
31. Model can do
validations
> class User
validates_presence_of :name
validates_numericality_of :age
end
> user = User.new(:age => 'thirty")
> user.save # fail
> user.errors.messages
http://guides.rubyonrails.org/active_record_validations_callbacks.html
Saturday, April 13, 13
32. Model can have
callbacks
• Callbacks are operations which hooked
into the lifecycle of an ActiveRecord object
• Object's lifecycle includes:
creating, updating and destroying an object
after initializing and finding an object
http://guides.rubyonrails.org/active_record_validations_callbacks.html
Saturday, April 13, 13
33. Callbacks for creating
an object
• before_validation
• after_validation
• before_save
• around_save
• before_create
• around_create
• after_create
• after_save
Saturday, April 13, 13
34. Callbacks for updating
an object
• before_validation
• after_validation
• before_save
• around_save
• before_update
• around_update
• after_update
• after_save
Saturday, April 13, 13
35. Callbacks for
destroying an object
• before_destroy
• around_destroy
• after_destroy
Saturday, April 13, 13
36. Model can have
associations
• One-to-one
• One-to-many
• Many-to-many
• and more...
http://guides.rubyonrails.org/association_basics.html
Saturday, April 13, 13
43. The rails command
• create new project
> rails new [project_name]
• start server
> rails server
• start console
> rails console
• execute generator
> rails generate [generator_type]
• more, and get help
> rails help [command]
Saturday, April 13, 13
68. Assignment 1:
Add a search bar above the book list
table on the index page, so the user can
search for books by author name or by
book title.
Saturday, April 13, 13
69. Assignment 2:
Add pagination links below the book
list table on the index page, so the user
can browse through 20 books per page.
Saturday, April 13, 13