Created
January 31, 2011 20:36
-
-
Save jonpaul/804754 to your computer and use it in GitHub Desktop.
rubytutorial.org microposts invalid SQL statement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% 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> | |
. | |
. | |
. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PagesController < ApplicationController | |
def home | |
@title = "Home" | |
if signed_in? | |
@micropost = Micropost.new | |
@feed_items = current_user.feed.paginate(:page => params[:page]) | |
end | |
end | |
. | |
. | |
. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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