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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:10:26+00:00 2026-06-13T11:10:26+00:00

My friend helped me to do some cleanup – but it doesn’t look much

  • 0

My friend helped me to do some cleanup – but it doesn’t look much like rails active record way.

first i wrote this way:

@count_users_approved = current_agent.users.map { |u| u.orders.where("created_at >= ? AND order_status_id = ?", DateTime.now.beginning_of_day, "3")}.count

# rather counting the records, the result was displaying in this way : [0,0,1]

My friend fixed it by writing it in this way:

@count_users_approved = Order.where("created_at >= ? AND user_id IN (?) AND order_status_id = ?", DateTime.now.beginning_of_day, current_agent.users.map(&:id), 2).count

can we write this much cleaner and ActiveRocord type ?

This is how my models are designed.

class Agent < ActiveRecord::Base
  has_many :users
end

class User < ActiveRecord::Base
  belongs_to :agent
  has_many :orders
  has_many :support_histories
  has_one :card_info
end

class Order < ActiveRecord::Base
  belongs_to :user
  belongs_to :order_type
  belongs_to :order_status
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-13T11:10:27+00:00Added an answer on June 13, 2026 at 11:10 am

    I’d go this way :

    @count_users_approved = Order.joins(:user).where("orders.created_at >= ?", DateTime.now.beginning_of_day).where(users: {agent_id: current_agent.id}, order_status_id: 2).count
    

    You’re joining the user table to test that the user is assigned to the current agent.

    Also there’s no need to map for ids, you could have just passed the current_agent.users array, Rails extracts ids of objects for you when building the query.

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

Sidebar

Related Questions

I was trying to clean up some code that a friend helped me with
I was helping a friend to write some Java code, who doesn't know a
I have a Rails 2 app that a friend (and way better rails dev)
I'm working on a C project with some friends for school. They helped me
My friend gave me a database file: record.mdf . I copied that .mdf file
For some reason, one of my models in rails is returning nil when I
Recently, my friend helped me fix a background overlay problem. I had an issue
A friend wrote some code for me, and there was one file with a
I just helped a friend to set up a Github account to collaborate on
Okay - yes, this is homework, but it isn't mine. I have a friend

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.