Which ORM (Object-relational mapping) library should I use? What should I use for my template language? Should I use MVC (Model/View/Controller) or some other pattern? What unit testing framework should I use? What database engine should I use?
Unfortunately, these questions make it difficult to start web projects. Therefore, Ruby on Rails has made all of these decisions already; you can just get started. And it's extensible enough that if one of the default choices doesn't work for you, just choose a different library. How does Ruby on Rails answer these questions? How does it use the Ruby language to help make your development even faster? Come and find out!
47. ActionPack
ActionDispatch
class PostsController < ApplicationController
def index
@posts = Post.order("created_at DESC").
limit(10)
end
end
48. ActionPack
ActionDispatch Inheritence
class PostsController < ApplicationController
def index
@posts = Post.order("created_at DESC").
limit(10)
end
end
49. ActionPack
ActionDispatch
Instance Method
class PostsController < ApplicationController
def index
@posts = Post.order("created_at DESC").
limit(10)
end
end
50. ActionPack
ActionDispatch
Instance Variable (field/private variable)
class PostsController < ApplicationController
def index
@posts
= Post.order("created_at DESC").
limit(10)
end
end
51. ActionPack
ActionDispatch
Class Class method (static)
class PostsController < ApplicationController
def index
@posts = Post.order("created_at DESC").
limit(10)
end
end
60. ActiveRecord
Post.find_by_title "Awesomest post ever!"
SELECT * FROM posts WHERE title = 'Awesomest post ever!'
61. ActiveRecord
Post.where("title = 'Awesomest post ever!'")
SELECT * FROM posts WHERE title = 'Awesomest post ever!'
62. ActiveRecord
Post.joins(:authors).where("authors.name = 'Joe'")
SELECT * FROM posts INNER JOIN authors ON
posts.author_id = authors.id
WHERE authors.author_name = 'Joe'
63. ActiveRecord
Post.find_by_title "Awesomest post ever!"
vs.
Post.where("title = 'Awesomest post ever!'")
64. ActiveRecord
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.string :title
t.text :body
t.timestamps
end
end
def self.down
drop_table :posts
end
end
108. ActionPack
ActionDispatch
class PostsController < ApplicationController
respond_to :xml
def index
respond_with(@posts = Post.
order("created_at DESC").
limit(10))
end
end
123. ActionPack
ActionDispatch
class PostsController < ApplicationController
respond_to :xml, :json
def index
respond_with(@posts = Post.
order("created_at DESC").
limit(10))
end
end
TAKE IT SLOW
Pause after topics
Take some deep breaths
Walk to other side of projector when pausing
Within3
- Ecommerce Sites on Shopify
- iPad/iPhone R&D
- Consulting
- Ecommerce Sites on Shopify
- iPad/iPhone R&D
- Consulting
If I throw out a term that you don&#x2019;t understand, please feel free to ask!
If I throw out a term that you don&#x2019;t understand, please feel free to ask!
If I throw out a term that you don&#x2019;t understand, please feel free to ask!
If I throw out a term that you don&#x2019;t understand, please feel free to ask!
Write once, use everywhere
Code is easier to consume
Easier to edit later
How many have not used MVC?
It&#x2019;s up to you to architect and organize your code in those tools. Rails makes that decision for you by implementing MVC.
Code driving the UI that gets displayed to the user
Interacts with model and sends the data to the view
- Open source framework for developing web applications
- Created by David Heinemeier Hansson
- 1.0 was an extremely opinionated framework
- 3.0 allows you to change many of the opinions
&#x2026;Written in the language
- Everything has a type, but the language infers types at runtime instead of you specifying them up front
Remembering names is not important; knowing how the pieces fit together & interact is
Utility collection and standard Ruby core extensions used both by Rails itself and your application
The glue; ties together framework libraries and 3rd party plugins
Build an application through the life of an HTTP request
- List 10 most recent posts, newest first
...since for most of you this is your first time seeing Ruby code, let&#x2019;s look at this a little bit
any instance variables in a controller are automatically handed into the view
...since for most of you this is your first time seeing Ruby code, let&#x2019;s look at this a little bit
ActiveRecord is....
Default option, but you can use any ORM that implements ActiveModel
Inheriting from ActiveRecord::Base does some pretty cool things for us
Because I have a class called Post AR knows to look up...
it also gives us some more time saving methods like...
find_by_title will call SQL right away
.where (and other query methods) will delegate the SQL call to the first time the array is used
Way to go forward and backwards between versions of your database
We didn&#x2019;t have to write a single line of data access code
&#x2026;here&#x2019;s some more strange Ruby syntax
this helper method generates the html for a link
this helper method generates the html for a link
&#x2026;this generates a number of helpful routes for us
...since for most of you this is your first time seeing Ruby code, let&#x2019;s look at this a little bit
params[:id] comes from resources route
ActiveRecord is....
Default option, but you can use any ORM that implements ActiveModel
We still haven&#x2019;t written a single line of data access code
&#x2026;here&#x2019;s some more strange Ruby syntax
&#x2026;this generates a number of helpful routes for us
...since for most of you this is your first time seeing Ruby code, let&#x2019;s look at this a little bit
ActiveRecord is....
Default option, but you can use any ORM that implements ActiveModel
We still haven&#x2019;t written a single line of data access code
With builder all of our block parameters become parent XML tags and all of our method calls child tags; we define attributes by passing a hash to a tag method
&#x2026;what if we wanted to enable easy Javascript interaction?
&#x2026;this generates a number of helpful routes for us
...since for most of you this is your first time seeing Ruby code, let&#x2019;s look at this a little bit
ActiveRecord is....
Default option, but you can use any ORM that implements ActiveModel
We still haven&#x2019;t written a single line of data access code
respond_with tells ActionController to format the object if it can
Rails can convert any object into JSON
We&#x2019;ve barely scratched the surface of what Rails can do.
Convention is very powerful, but sometimes it makes sense to override the defaults
Rails gets most of its flexibility from the Ruby language. Uses &#x201C;metaprogramming&#x201D; to generate code at runtime. Optimized for the best of productivity & performance.
You&#x2019;d be surprised to see who&#x2019;s using Rails.
Highly recommend some further reading to get a better feel for how you can make Rails work for you.