Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8283149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:38:08+00:00 2026-06-08T10:38:08+00:00

I know I’m asking for a lot in this post but after reading 4

  • 0

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

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

end

Relationship

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id

  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"
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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:38:10+00:00Added an answer on June 8, 2026 at 10:38 am

    Hm, what’s your concrete question? You simply don’t have to look at the complicated User associations, that as a hint! (I found them confusing, too, with all the followed and and followed by. But the change mainly goes into the Micropost model, so you should start with that following the hints that Michael Hartl gives.

    If you want to, you can peek at my solution which I added 14 days ago at

    https://github.com/htw-rails/TutorialSampleApp32/

    • some of the source code might look a bit different, as this version has grown out of the Tutorial for the Rails 3.0 version.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this sort of question has been asked here before, but still
I know that this may be common knowledge, but is there a way to
know nothing about php, but I have this script that reads a folder and
I know that there has been one question about this but it is not
Know this might be rather basic, but I been trying to figure out how
I know that Phonegap has an event for back button, but it's only available
Anyone know how I can make a login button in Ruby on Rails for
i know this is a stupid question but i d'ont know how to do
I know this is a stupid question, but I've looked for 45 mins now
I know that design patterns is generally something that's connected to OO programming, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.