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 8213973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:18:05+00:00 2026-06-07T11:18:05+00:00

I finished Hartl’s tutorial and have been trying to add twitter-like @replies for the

  • 0

I finished Hartl’s tutorial and have been trying to add twitter-like @replies for the past few days to no avail. I added an in_reply_to column to the Microposts table as an integer that I thought could be used to reference a given user’s id. As of now I’m using a regex to search for a match with a given user’s name via the Microposts controller.

Hartl suggests using an including_replies scope in the Micropost model. I’ll admit I’m not quite sure what to include in this scope based on what automatic associations rails creates or what I have to tell it.

Any assistance would be greatly appreciated.

User Model

  has_many :microposts, dependent: :destroy
  has_many :replies, through: :microposts, source: :in_reply_to

  VALID_NAME_REGEX =  /\A[\w+\-.]\z/i
  validates :name, presence: true,
            format: { with: VALID_NAME_REGEX },
            length: { maximum: 20 },
            uniqueness: { case_sensitive: false }

...

  def feed
    Micropost.from_users_followed_by(self)
    Micropost.including_replies
  end

Micropost Model

class Micropost < ActiveRecord::Base
  attr_accessible :content
  belongs_to :user
  belongs_to :in_reply_to, class_name: "User"

  validates :user_id, presence: true
  validates :content, presence: true, length: { maximum: 140 }

  default_scope order: 'microposts.created_at DESC'
  scope :including_replies, where("user_id = in_reply_to")

  def self.from_users_followed_by(user)
    followed_user_ids = "SELECT followed_id FROM relationships
                         WHERE follower_id = :user_id"
    where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
          user_id: user.id)
  end
end

Microposts Controller

class MicropostsController < ApplicationController
  before_filter :signed_in_user, only: [:create, :destroy]
  before_filter :correct_user,   only: :destroy
  before_filter :reply_to_user, only: :create

  def create
    @micropost = current_user.microposts.build(params[:micropost])
      if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to root_path
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end

  def destroy
    @micropost.destroy
    redirect_to root_path
  end

  private

    def correct_user
      @micropost = current_user.microposts.find_by_id(params[:id])
      redirect_to root_path if @micropost.nil?
    end

    def reply_to_user
      if reply_to = @micropost.content.match(/\A(@[\w+\-.])\z/i)
      @other_user = User.where(name: reply_to.to_s[1..-1])
        if @other_user && current_user.followed_users.includes(@other_user)
        @micropost.in_reply_to = @other_user.id
        end
      end
    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-07T11:18:06+00:00Added an answer on June 7, 2026 at 11:18 am

    I’ve started to look into this and the first thing I’ve noticed is that your regexp looking for replies doesn’t work:

    > /\A(@[\w+\-.])\z/i.match("@someName blabla")
    => nil 
    

    So the first tip: test every bit you’re implementing.
    Also, I think you got the association wrong, I used

       has_many :replies, foreign_key: "to_id", class_name: "Micropost"
    

    (I’ve renamed the key from in_reply_to to simply “to” because there was a strange deprecation warning when using the underscores).

    but you mainly asked how to define the new scope in Micropost. I did it like this:

    scope :from_users_followed_by_including_replies, lambda { |user| followed_by_including_replies(user) }
    

    …

    def self.followed_by_including_replies(user)
      followed_ids = %(SELECT followed_id FROM relationships
                       WHERE follower_id = :user_id)
      where("user_id IN (#{followed_ids}) OR user_id = :user_id OR to_id = :user_id",
            { :user_id => user })
     end
    

    I hope that helps. you can have a look at my whole implementation at

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I finished my small application and I am trying to make sure I have
Have been following Rails Tutorial by Michael Hart rails version 3.0 on mac OS
I'm trying to follow Michael Hartl's Ruby on Rails Tutorial in http://ruby.railstutorial.org/chapters/sign-in-sign-out , but
enter code hereI'm trying to finish up Michael Hartl's Ruby on Rails v3.2 Tutorial
I finished my course on Oracle databases and have been playing with it since
I am new to Rails and finished Michael Hartl's Ruby on Rails 3 Tutorial.
Just finished Michael's Hartl online tutorial . So my current state is: a little
i m following michael hartl's ruby on rails online tutorial book and finished chapter
I finished NerdDinner tutorial and now I'm playing a bit with project. Index page
I finished building a photo gallery and would like to give users the ability

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.