3. Rails 3: Overview
Merb
Fast
Lightweight
Powerful = featureful, flexible, and extensible
Rails
Popular
Easy to get started
Opinionated
Large ecosystem: books, blogs, screencasts, community
5. Ruby Version Manager (RVM)
Why?
For me, I've been attached to Ruby 1.8.6
for a long time. Mostly because it is stable
and legacy projects I work on rely on it.
Lots of gems installed that just work.
Rails 3 requires 1.8.7 and above! Doh!!!
6. Ruby Version Manager (RVM)
Command line tool which allows us to
easily install, manage and work with
multiple ruby environments (and
associated gems).
8. Ruby Version Manager (RVM)
Installation:
> mkdir -p ~/.rvm/src/ && cd ~/.rvm/src && rm -rf ./rvm/ && git
clone --depth 1 git://github.com/wayneeseguin/rvm.git && cd rvm
&& ./install
That is a long command, eh.
You might want to go here and read for yourself: http://rvm.
beginrescueend.com/rvm/install/
Follow instructions after that which involves adding a line to
your bash profile.
9. Installing Rails Using RVM
> rvm install 1.9.1 (or 1.8.7 or 1.9.2)
> rvm 1.9.1 --default (uses 1.9.1 on newly opened terminal windows)
> curl -L http://rvm.beginrescueend.com/gemsets/rails3.gems -o rails3.gems
> rvm gemset import rails3.gems
> rails -v (should show Rails 3 beta)
You may have to install sqlite/mysql and associated gems. For
example,
> gem install sqlite3-ruby
10. Ruby Version Manager (RVM)
A few more things:
To use system ruby installed by default:
> rvm system --default
See installed rubies:
> rvm list
Named Gem Sets
RVM not only isolates different ruby installations it also lets you
isolate different gem sets. Thus you can mix and match rubies
and gem sets. Amazing!! More here: http://rvm.
beginrescueend.com/gemsets/basics/
11. Introduction to Rails 3
Generate a Rails 3 app
> rails generate app_name
> rails g app_name
script/rails is the only thing in there!
Generate scaffold
> rails g scaffold Post title:string body:text
> rake db:migrate
Start server
> rails server
> rails s
12. Introduction to Rails 3
Console
> rails console
> rails c
Database Console
> rails dbconsole
> rails db
13. Introduction to Rails 3
ActiveRecord Validation
Rails 2 - separate validations
validates_presence_of :title
validates_uniqueness_of :title
validates_length_of :title, :maximum => 35
Rails 3 - puts validations together for a field
validates :title, :presence => true,
:uniqueness => true,
:length => {:maximum => 35}
14. Introduction to Rails 3
ActiveRecord Query Interface
New API
where(:conditions)
having(:conditions)
select
group
order
limit
includes(:include)
and more...
15. Introduction to Rails 3
ActiveRecord Query Interface
Why New API?
Chainability!!!! No not gold chains. :)
Post.where(:title => "test")
Post.where(:title => "test").order("created_at DESC")
Post.order("created_at").limit(10)
Post.where("created_at <= ?", Time.now).includes(:comments)
16. Introduction to Rails 3
ActiveRecord Query Interface
Lazy Loading
posts = Post.all
Returns a ActiveRelation object containing information about query.
As soon as you access ActiveRelation object, for example in view,
then call gets executed to db. Nice for fragment caching, etc.
<% posts.each do |post| %>
<li><%= post.title %></li>
<% end %>
17. Introduction to Rails 3
More Rails 3 Goodies:
Router
Mailer
Unobtrusive Javascript
...
Will have to wait for another presentation. :)
18. Introduction to Rails 3
All you want to know and more about Rails 3
http://mediumexposure.com/rails-3-reading-material/
http://www.rubyinside.com/rails-3-0-beta-links-2966.html
http://railslove.com/weblog/2010/02/02/on-the-way-to-rails-
3-a-link-list/
http://railsnotes.com/rails-3/
19. References
http://www.slideshare.net/GreggPollack/rails-3-beautiful-
code-3219240
http://guides.rails.info/getting_started.html
http://railscasts.com/episodes/202-active-record-queries-in-
rails-3
http://m.onkey.org/2010/1/22/active-record-query-interface
http://rvm.beginrescueend.com/
pik
rvm tool on windows, I've never used though
http://github.com/vertiginous/pik