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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:53:18+00:00 2026-05-24T08:53:18+00:00

I created a method on a micropost model which takes an array of user_ids.

  • 0

I created a method on a micropost model which takes an array of user_ids. In this method I use the following ‘find’ method to pull all posts for all of the users within the array.

find(:all, :conditions => ["user_id IN (?)", args.join(',')])

But when ActiveRecord generates the SQL for this query it surrounds the coma delimited list of Ids in single quotes. This causes the query to only pull posts for the first number within the single quotes instead of all the numbers.

SELECT `microposts`.* FROM `microposts` WHERE (user_id IN ('3,4,5')) ORDER BY microposts.created_at DESC

The query should look like this to work correctly but I can’t figure out how to get Ruby to convert the Array to a coma delimited list without the quotes, I know it has to be something simple and I am just missing it and can’t find anything on Google that fixes this. All the posts I have found use the same .join(‘,’) method to concert the Array.

SELECT `microposts`.* FROM `microposts` WHERE (user_id IN (3,4,5)) ORDER BY microposts.created_at DESC

One more fact which migh assist you in figuring out my issue is the Array of ids which I am passing into the method is being built in the following manner. Not sure if this would affect things.

ids = Array.new
ids.push(current_user.id)
ids = ids + current_user.friends.map(&:id)
ids = ids + current_user.reverse_friends.map(&:id)

Thanks in advance to the one who can help me get back to work 😉

  • 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-05-24T08:53:19+00:00Added an answer on May 24, 2026 at 8:53 am

    Answer updated (twice) to reflect Rails 3+ ActiveRecord syntax

    Rails 3+

    You can use the following syntax:

    Post.where(user_id: [1,2,3])
    

    And thus:

    Post.where(user_id: args)
    

    Rails 3 or Rails 4 (Original)

    For completeness, the Rails 3 syntax would be:

    Post.where("user_id IN (?)", [1,2,3]).to_a
    

    If args is an Array, then:

    Post.where("user_id IN (?)", args).to_a
    

    Or, if args is an array of strings:

    Post.where("user_id IN (?)", args.map(&:to_i)).to_a
    

    The to_a is optional, but will return the results as an array. Hope this helps.

    Rails 2

    You should be able to simply pass in an array as the parameter, as follows:

    find(:all, :conditions => ["user_id IN (?)", [1,2,3] ] )
    

    Thus, if args is an array of ids, you should simply call:

    find(:all, :conditions => ["user_id IN (?)", args ] )
    

    In case you have an issues with types, you can do:

    find(:all, :conditions => ["user_id IN (?)", args.map { |v| v.to_i } ] )
    

    and this will ensure each item in the array is an Integer.

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

Sidebar

Related Questions

I created a Micropost model that have the following attributes: <Micropost id: 1, content:
I'm trying to learn about Expression trees, and I've created a method that takes
I created the following method for retrieving stored settings from the database: public String
I've created a web service , which can a method to set the user
I created a Winforms AutoComplete combobox control inheriting from ComboBox with the following method:
I've created a method that generates a new class and adds some methods into
I have Dropdownlist on my page and its selectedindexchanged method created in code behind
Given a DataSet, I'm left joining DataTables[1-n] onto DataTable[0]. I have created a method
I created an extension method called HasContentPermission on the System.Security.Principal.IIdentity : namespace System.Security.Principal {
We've created a generic method like so: public TReturnType GetValue(string key) { var configurationParameter

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.