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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:39:12+00:00 2026-06-01T06:39:12+00:00

I am learning rails from rails tutorial. I got a problem in the Section

  • 0

I am learning rails from rails tutorial. I got a problem in the Section 11.3.1, the code in the Listing 11.44:

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

will not return user_id in followed_user_ids.

After searching at stackoverflow, I found the solution here, which said that just remove the "join" method and it works for me.

But I am curious about why there is no error when I run RSPEC before remove the "join" method.

In Listing 11.41:

subject { Micropost.from_users_followed_by(user) }

it { should include(own_post) }
it { should include(followed_post) }

If the "join" method will make the SQL statement wrong, why RSPEC passed?

Does anyone have the same problem?

Update 2012.04.03

I used rails console to check the problem.

user = User.first
ids = ids = user.following_users.map(&:id).join(', ')

Which got

User Load (2.6ms)  SELECT "users".* FROM "users" INNER JOIN "follows" ON
"follows"."followable_id" = "users"."id" AND "follows"."followable_type" = 'User' WHERE    
"follows"."blocked" = 'f' AND "follows"."follower_id" = 1 AND "follows"."follower_type" 
= 'User' AND "follows"."followable_type" = 'User'
=> "6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29,    
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 
52, 55, 56, 5"

Then the SQL

Collection.where("user_id IN (?)", ids)

The result is

Collection Load (0.7ms)  SELECT "collections".* FROM "collections" WHERE (user_id IN 
('6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 
52, 55, 56, 5')) ORDER BY collections.created_at DESC
=> []

Which return an empty array.

But I got all my rspec passed. Still no idea.

  • 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-01T06:39:13+00:00Added an answer on June 1, 2026 at 6:39 am

    Assuming the join creates a string '1,2,3', then perhaps you want to join("','") to have ('1','2','3'), but I don’t think this will work – I’m sure PostgreSQL doesn’t like IDs as strings.

    Alternatively:

    Squeel is a good SQL syntax gem where you could use:

    user = User.first
    ids = user.following_users.map(&:id)
    Collection.where{(user_id.like_any ids)}
    

    This will allow you to pass in the array of integers and Squeel will setup the query where:

    SELECT "collections".* FROM "collections" WHERE (("collections"."user_id" LIKE 1 OR "collections"."user_id" LIKE 2 OR "collections"."user_id" LIKE 3))
    

    EDIT: You can also use the syntax:

    ids = User.first.following_users.pluck(:id)
    Collection.find_by_user_id(ids)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning rais from the rails tutorial book: http://ruby.railstutorial.org/chapters/sign-in-sign-out#fnref:9.14 I am working on
I am learning ruby from this guide http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:deploying , and I am trying to
This is a beginner's rails learning confusion. When I learn rails, from time to
As I am learning more about rails, and breaking my design thinking from ASP.Net
Im learning rails and exploring a bit away from the book and creating a
Its quite a while since I started learning Java EE (I come from Rails
I am learning rails by following the Rails Tutorial under Ubuntu. I have been
I'm new to Rails and i'm currently learning from tutorials from http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-one#top find_by_email works
A few years back, I started learning Ruby/Rails from some beginner's guide for Rails.
I'm in the process of learning Ruby on Rails, and I'm coming from just

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.