Ruby on Rails 3 Tutorial exercise no. 1 hints
===

I know I'm asking for a lot in this post but after reading 4 books on Ruby/Rails I'm frustrated by the fact that I'm not getting the "aha" moment.  If someone can help I'll come over and cook you breakfast (for a week).

I'm coming from the world of PHP/MySQL and I'm finding it difficult to grasp certain things in Rails.  The last book I read by Michael Hartl suggests some exercises to add to the application he's built in the book.  It has to do with associations.  So I was wondering if someone could give me some hints how to go about this because I'm really stuck.

The application he builds is almost a Twitter clone.  There are Users who post Microposts.  Their home page looks like this http://ruby.railstutorial.org/chapters/following-users#fig:home_page_with_feed  The User's own Microposts are posted along the right hand side in the 'feed'.  Along with the User's Microposts in the feed are also Microposts by Users that are being followed by the current User.  You can follow and unfollow any User you want.

The exercise suggests adding @replies.  A @reply is a Micropost starting with @username (e.g. '@mikeglaz how are you').  This Micropost will then appear in your feed and the username's feed (not necessarily someone you're following).  The author suggests the following: 'This might involve adding an in_reply_to column in the microposts table and an extra including_replies scope to the Micropost model.'  But the associations regarding following other users are pretty complex and this is what's keeping me stuck.  I'll post some code:

**User**

    class User < ActiveRecord::Base
      attr_accessible :email, :name, :password, :password_confirmation
      has_secure_password
      has_many :microposts, dependent: :destroy
      has_many :relationships, foreign_key: "follower_id", dependent: :destroy
      has_many :followed_users, through: :relationships, source: :followed
      has_many :reverse_relationships, foreign_key: "followed_id",
           class_name:  "Relationship",
           dependent:   :destroy
      has_many :followers, through: :reverse_relationships, source: :follower
    
      def feed
        Micropost.from_users_followed_by(self)
      end

      def follow!(other_user)
        relationships.create!(followed_id: other_user.id)
      end
    end

**Relationship**

    class Relationship < ActiveRecord::Base
      attr_accessible :followed_id

      belongs_to :follower, class_name: "User"
      belongs_to :followed, class_name: "User"

      def unfollow!(other_user)
        relationships.find_by_followed_id(other_user.id).destroy
      end
    end

**Micropost**

    class Micropost < ActiveRecord::Base
      attr_accessible :content
      belongs_to :user

      def self.from_users_followed_by(user)
        followed_user_ids = user.followed_user_ids
        where("user_id IN (?) OR user_id = ?", followed_user_ids, user)
      end
    end