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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:49:09+00:00 2026-06-09T19:49:09+00:00

I am working on a rails project for a while and I juste encountered

  • 0

I am working on a rails project for a while and I juste encountered (what I think is) a small problem.

To make it simple, I have 3 model like this:

 class House < ActiveRecord::Base
  has_many :house_style_relationships
 end

 class HouseStyleRelationship < ActiveRecord::Base
  attr_accessible :percentage

  belongs_to :style
  belongs_to :house
 end

class Style < ActiveRecord::Base
  has_many :house_style_relationships
end

So the HouseStyleRelationship model simply let us know the style percentage of an House.
For example an House can be 70% Modern and 30% Classic.

What I need to do is to get the average of each styles for all the houses.
To do that I used this working query under SQLite:

houses_count = Houses.count
HouseStyleRelationship.joins(:style).select("style.name, SUM(house_style_relationships.percentage)/#{houses_count} as percentage").group("styles.id")

But when I decided to push all this stuff on Heroku, I got a problem with PostgreSQL.
In fact, to create an HouseStyleRelationship object (I don’t even need), ActiveRecord ask for a HouseStyleRelationship.id which make the Group By crashing (with the query).
(PostgreSQL don’t want to group records with different ids)

Do you have a solution to prevent ActiveRecord to generate model instances as answer and so remove the HouseStyleRelationship.id from the query ?
(I can’t use SELECT DISTINCT since I need the SUM() calculation)

Thanks in advance !

  • 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-09T19:49:10+00:00Added an answer on June 9, 2026 at 7:49 pm

    You need to create your join table such that it has no primary key. The create_table statement in your migration should look something like this.

    create_table :houses_style_relationships, :id => false do |t|
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on Ruby on rails project for a while now, when
Hi Currently i am working on a rails project . where i have to
I am working on a very simple project to learn Rails better, coming from
Hello Ruby/Rails/Merb developers! Im currently working on a web project that will have a
I am working on a Rails project and hitting a performance problem. Here is
I have been working on rails project, which had been made using noSql(Mongoid). Every
I am working on an existing Ruby on Rails project. While one of the
I'm working on a Rails project in which I have a somewhat of an
I'm currently working on a Rails project, and have found times where it's easiest
We're working on a Rails 3 project and testing using Capybara/RSpec. The problem is

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.