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

  • Home
  • SEARCH
  • 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 7533027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:37:24+00:00 2026-05-30T05:37:24+00:00

Example class User has_many :tickets end I want to create association which contains logic

  • 0

Example

class User  
  has_many :tickets 
end

I want to create association which contains logic of count tickets of user and use it in includes (user has_one ticket_count)

Users.includes(:tickets_count)

I tried

  has_one :tickets_count, :select => "COUNT(*) as tickets_count,tickets.user_id " ,:class_name => 'Ticket', :group => "tickets.user_id", :readonly => true  

User.includes(:tickets_count)

 ArgumentError: Unknown key: group

In this case association query in include should use count with group by …
How can I implement this using rails?

Update

  • I can’t change table structure
  • I want AR generate 1 query for collection of users with includes

Update2

I know SQL an I know how to select this with joins, but my question is now like “How to get data” . My question is about building association which I can use in includes. Thanks

Update3
I tried create association created like user has_one ticket_count , but

  1. looks like has_one doesn’t support association extensions
  2. has_one doesn’t support :group option
  3. has_one doesn’t support finder_sql
  • 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-30T05:37:25+00:00Added an answer on May 30, 2026 at 5:37 am

    Try this:

    class User
    
      has_one :tickets_count, :class_name => 'Ticket', 
        :select => "user_id, tickets_count",
        :finder_sql =>   '
            SELECT b.user_id, COUNT(*) tickets_count
            FROM   tickets b
            WHERE  b.user_id = #{id}
            GROUP BY b.user_id
         '
    end
    

    Edit:

    It looks like the has_one association does not support the finder_sql option.

    You can easily achieve what you want by using a combination of scope/class methods

    class User < ActiveRecord::Base
    
      def self.include_ticket_counts
        joins(
         %{
           LEFT OUTER JOIN (
             SELECT b.user_id, COUNT(*) tickets_count
             FROM   tickets b
             GROUP BY b.user_id
           ) a ON a.user_id = users.id
         }
        ).select("users.*, COALESCE(a.tickets_count, 0) AS tickets_count")
      end    
    end
    

    Now

    User.include_ticket_counts.where(:id => [1,2,3]).each do |user|
      p user.tickets_count 
    end
    

    This solution has performance implications if you have millions of rows in the tickets table. You should consider filtering the JOIN result set by providing WHERE to the inner query.

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

Sidebar

Related Questions

For example class User < ActiveRecord::Base has_one :avatar, :dependent => :destroy end class Avatar
I have a model defined as below: class Example(models.Model): user = models.ForeignKey(User, null=True) other
I have a User model that has_many parents. I want that user model to
i have following code: class Item < ActiveRecord::Base belongs_to :user has_many :transactions #scope :active,
Example class in pseudocode: class SumCalculator method calculate(int1, int2) returns int What is a
Example: class MyClass { Composition m_Composition; void MyClass() { m_Composition = new Composition( this
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):
I have the following example class: Test.h: @interface Test : UIButton { NSString *value;
Below is an example class hierarchy and code. What I'm looking for is a

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.