Skip to content

Instantly share code, notes, and snippets.

@amiel
Created April 23, 2010 02:58
Show Gist options
  • Save amiel/376118 to your computer and use it in GitHub Desktop.
Save amiel/376118 to your computer and use it in GitHub Desktop.
Talk.all # SELECT * FROM "talks"
Talk.last # SELECT * FROM "talks" ORDER BY talks.id DESC LIMIT 1
# Rails 2
Presenter.all :conditions => { :name => "Amiel Martin" }, :order => 'created_at ASC'
# Rails 3
Presenter.where(:name => "Amiel Martin").order("created_at ASC").all
amiel = Presenter.find_by_name("Amiel Martin")
# SELECT * FROM "presenters"
# WHERE ("presenters"."name" = 'Amiel Martin') LIMIT 1
amiel.talks # SELECT * FROM "talks" WHERE ("talks".presenter_id = 1)
amiel.email = "[email protected]"
amiel.save
# UPDATE "presenters"
# SET "updated_at" = '2010-04-24 17:33:41', "email" = '[email protected]'
# WHERE "id" = 1
Talk.create :title => "Intro to Rails", :presenter => amiel
# INSERT INTO "talks" ("title", "presenter_id", "created_at", "updated_at")
# VALUES('Intro to Rails', 1, '2010-04-24 17:34:46', '2010-04-24 17:34:46')
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><%= render_title %></title>
</head>
<body>
<div id="wrapper">
<h1>#bhamruby</h1>
<%= yield %>
</div>
</body>
</html>
<h2>Linux Fest Talks</h2>
<ol>
<%- for talk in @talks -%>
<li style="background-image:
url(<%= talk.presenter.gravatar_url %>);">
<%= talk.title %>
<span class="presenter">
<%= talk.presenter.name %>
</span>
</li>
<%- end -%>
</ol>
# presenter.rb
class Presenter < ActiveRecord::Base
has_many :talks
end
# talk.rb
class Talk < ActiveRecord::Base
belongs_to :presenter
end
class Presenter < ActiveRecord::Base
has_many :talks
def gravatar_url(size = 64)
"http://www.gravatar.com/avatar/" +
Digest::MD5.hexdigest(self.email) +
".jpg?s=#{size}"
end
end
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>#bhamruby Linux Fest Talks</title>
</head>
<body>
<div id="wrapper">
<h1>#bhamruby</h1>
<h2>Linux Fest Talks</h2>
<ol>
<li>Introduction to Rails <span class="presenter">Amiel Martin</span></li>
<li>Front End Development <span class="presenter">Nathan Carnes</span></li>
<li>Making Rails Interesting <span class="presenter">James Mason</span></li>
<li>Deploying and Scaling Rails <span class="presenter">Adrian Pike</span></li>
</ol>
</div>
</body>
</html>
# everything is an object
[12, 47, 35].max # => 47
3.hours.ago # => Sat, 24 Apr 2010 07:11:28 PDT -07:00
"person".pluralize # => "people"
5.times { print "Foo Bar" }
people.each do |person|
puts person.name unless person.private?
end
class Talk < ActiveRecord::Base
set_table_name 'talks'
set_primary_key 'id'
belongs_to :presenter,
:foreign_key => 'presenter_id',
:class_name => 'Presenter'
end
class TalksController < ApplicationController
def index
# load the data
@talks = Talk.all
# SELECT * FROM "talks"
# render the view
render "index"
end
end
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Lorem Ipsum</title>
</head>
<body>
<div id="wrapper">
<h1>Lorem Ipsum</h1>
<h2>Lorem ipsum dolor sit amet</h2>
<ol>
<li>consectetur adipisicing elit <span>sed do eiusmod</span></li>
<li>tempor incididunt ut labore <span>et dolore</span></li>
<li>Ut enim ad minim veniam <span>quis nostrud</span></li>
<li>exercitation ullamco <span>laboris nisi</span></li>
</ol>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment