Skip to content

Instantly share code, notes, and snippets.

@jonpaul
Created January 31, 2011 20:36
Show Gist options
  • Save jonpaul/804754 to your computer and use it in GitHub Desktop.
Save jonpaul/804754 to your computer and use it in GitHub Desktop.
rubytutorial.org microposts invalid SQL statement
<% if signed_in? %>
<table class="front" summary="For signed-in users">
<tr>
<td class="main">
<h1 class="micropost">What's up?</h1>
<%= render 'shared/micropost_form' %>
<% unless @feed_items.empty? %>
<%= render 'shared/feed' %>
<% end %>
</td>
.
.
.
class Micropost < ActiveRecord::Base
attr_accessible :content
belongs_to :user
validates :content, :presence => true, :length => { :maximum => 140 }
validates :user_id, :presence => true
default_scope :order => 'microposts.created_at DESC'
#Return microposts from the users being followed by the given user--
scope :from_users_followed_by, lambda { |user| followed_by(user) }
def self.from_users_followed_by(user)
followed_ids = user.following.map(&:id).join(", ")
where("user_id IN (#{followed_ids}) OR user_id = ?", user)
end
private
#Return an SQL condition for users followed by the given user.
#We include the user's own ID as well--
def self.followed_by(user)
followed_ids = %(SELECT followed_id FROM relationships
WHERE follower_id = :user_id)
where("user_id IN (#{followed_ids}) OR user_id = :user_id",
{ :user_id => user })
end
end
class PagesController < ApplicationController
def home
@title = "Home"
if signed_in?
@micropost = Micropost.new
@feed_items = current_user.feed.paginate(:page => params[:page])
end
end
.
.
.
class User < ActiveRecord::Base
.
.
.
def following?(followed)
relationships.find_by_followed_id(followed)
end
def follow!(followed)
relationships.create!(:followed_id => followed.id)
end
def unfollow!(followed)
relationships.find_by_followed_id(followed).destroy
end
def feed
Micropost.from_users_followed_by(self)
end
.
.
.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment