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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:57:09+00:00 2026-06-11T13:57:09+00:00

I have 2 models Category and Article related like this: class Category < ActiveRecord::Base

  • 0

I have 2 models Category and Article related like this:

class Category < ActiveRecord::Base
  has_many :articles
end

class Article < ActiveRecord::Base
  belongs_to :category

  def self.count_articles_per_category
    select('category_id, COUNT(*) AS total').group(:category_id)
  end
end

I’m accessing count_articles_per_category like this

Article.count_articles_per_category

which will return articles that have 2 columns: category_id and total.

My problem is that total column is a string. So the question is: is there a method to fetch that column as an integer?

PS: I tried to do a cast in the database for COUNT(*) and that doesn’t help.
I try to avoid doing something like this:

articles = Article.count_articles_per_category
articles.map do |article|
  article.total = article.total.to_i
  article
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-11T13:57:11+00:00Added an answer on June 11, 2026 at 1:57 pm

    No, there is no support in ActiveRecord to automatically cast datatypes (which are always transferred as strings to the database).

    The way ActiveRecord works when retrieving items is:

    • for each attribute in the ActiveRecord model, check the column type, and cast the data to that type.
    • for extra columns, it does not know what data type it should cast it to.

    Extra columns includes columns from other tables, or expressions.


    You can use a different query, like:

    Article.group(:category_id).count
    Article.count(:group => :category_id)
    

    These return a hash of :category_id => count. So you might get something like {6=>2, 4=>2, 5=>1, 2=>1, 9=>1, 1=>1, 3=>1}.

    Using the count method works because it implicitly lets ActiveRecord know that it is an integer type.

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

Sidebar

Related Questions

I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have the following models: Post.rb class Post < ActiveRecord::Base belongs_to :category has_many :attachments,
I have the model class Article < ActiveRecord::Base belongs_to :category end class Category <
i have two models: class Category has many :jobs end class Job belongs_to :category
Say I have two models, Article and Category: class Article(models.Model): category = models.ForeignKey(Category, related_name='articles')
I have three models: category.rb class Category include Mongoid::Document # Relationships has_many :posts, :autosave
I have two models, 'product' with 'belongs_to' and 'category' with 'has_many'. Product has a
I have the following Django 1.2 models: class Category(models.Model): name = models.CharField(max_length=255) class Article(models.Model):
Hey, I have models like this: class Galleries(models.Model): creation_date = models.DateTimeField() name = models.CharField(max_length=255,
I have 3 models: Category, Account, and SubAccount The relations are: Accounts has_many :sub_accounts

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.