Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails 3 Tutorial exercise no. 1 hints
    primarykey
    data
    text
    <p>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).</p> <p>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.</p> <p>The application he builds is almost a Twitter clone. There are Users who post Microposts. Their home page looks like this <a href="http://ruby.railstutorial.org/chapters/following-users#fig:home_page_with_feed" rel="nofollow">http://ruby.railstutorial.org/chapters/following-users#fig:home_page_with_feed</a> 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.</p> <p>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:</p> <p><strong>User</strong></p> <pre><code>class User &lt; 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 def unfollow!(other_user) relationships.find_by_followed_id(other_user.id).destroy end end end </code></pre> <p><strong>Relationship</strong></p> <pre><code>class Relationship &lt; ActiveRecord::Base attr_accessible :followed_id belongs_to :follower, class_name: "User" belongs_to :followed, class_name: "User" end </code></pre> <p><strong>Micropost</strong></p> <pre><code>class Micropost &lt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload