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

The Archive Base Latest Questions

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

I’m doing a rails app. I have to do a comparison engine a bit

  • 0

I’m doing a rails app. I have to do a comparison engine a bit complex. I’m currently trying to do a prototype. My query can vary widely so i have to work with a lot of scopes, but that’s not my problem.

My query have to compare candidates. These candidates have answered some tests. These tests belongs to category. Theses tests have different max value, and i have to be able to compare candidates by categories.

So i have to calculate a % of good answers. I have to be able to compare candidates in all possible use cases in one category. So, i have to be able to compare the average good answer rate for all this category.

In a nutshell : I have to be able to use subqueries in order to compare some candidates. I have to be able to compare them for a test or a category. My problem is using a subquery able to return a good answer rate for all tests a candidats may have passed in a category.

And I have to be able to use this subquery in an order_by or having clause.

How can I construct this subquery ? I have no problem to handle complex conditional queries with some scopes. This has to be a real subquery, because I am working with 6 or 7 models here.

I ask for an active record way, cause this must work with whatever database supported by rails.

Excuse my poor English.

Edit :

An example is worth 1000 words so how could do something like this :

Sessiontest.find(Candidat.where(:firstname => 'toto'))

This example is stupid, ok. So, is it possible to do something like this ?

Edit2 :

I saw some posts about AREL. I wish to know if it is possible to do this without a third party plugin.

Is it possible to do some sub queries in subqueries with arel? Because for example, my number of points per test, is the sum of the points of all his questions. (Sad, but I have to keep it). And I need this, so my subquery can calculate my good answers %.

So you got the idea. That’s something, which has to be really powerful, so I need something powerful, and not too much error prone.

Edit3 : I made some progress, but I can’t for a while post an answer.

It seem possible to get this work without any plugin. I have some success in buildings some subqueries like this :

 toto = Candidat.where(:lastname => Candidat.select(:lastname).where(:lastname => "ulysse").limit(1))

The request :

Candidat Load (1.0ms)[0m  SELECT "candidats".* FROM "candidats"    WHERE "candidats"."lastname" IN (SELECT "candidats"."id" FROM "candidats" WHERE "candidats"."lastname" = 'ulysse' LIMIT 1

This works and create a real subquery. I will try some more advanced experiences, in order to get the level I actually need.

Just tried sub-subquery works wonder too.

Edit 5 :

I am trying some more advanced things, and there is a lot of things, i still don’t understand.

- toto = Candidat.where("id = ? / ? ", Sessiontest.select(:id).where(:id => 6), Sessiontest.select(:id).where(:id => 2))

This is just a stupid example in order to get an object with an id of 3. This code works, but not as i expected.

See, the sql :

1m[35m (1.0ms)[0m  SELECT COUNT("sessiontests"."id") FROM "sessiontests" WHERE "sessiontests"."id" = 6
 [1m[36mSessiontest Load (0.0ms)[0m  [1mSELECT id FROM "sessiontests" WHERE "sessiontests"."id" = 6[0m
 [1m[35m (1.0ms)[0m  SELECT COUNT("sessiontests"."id") FROM "sessiontests" WHERE "sessiontests"."id" = 2
 [1m[36mSessiontest Load (1.0ms)[0m  [1mSELECT id FROM "sessiontests" WHERE "sessiontests"."id" = 2[0m
 [1m[35mCandidat Load (1.0ms)[0m  SELECT "candidats".* FROM "candidats" WHERE (id = 6 / 2)

So, it does not use a subqueries. I tried with .to_sql. But it introduce my sql this way :

1m[36mCandidat Load (0.0ms)[0m  [1mSELECT "candidats".* FROM "candidats" WHERE (id = 'SELECT id FROM "sessiontests" WHERE "sessiontests"."id" = 6' / 2 )[0m

So active record quoted the subreust for security purpose. this is closer to my wish, but not really what i want.

This does not work

Candidat.where("id = (?) / ? ", Sessiontest.select(:id).where(:id => 6).to_sql, Sessiontest.select(:id).where(:id => 2))

Quotes prevents the subquery to work.

But this work :

Candidat.where("id = (" + Sessiontest.select(:id).where(:id => 6).to_sql + ") / (" + Sessiontest.select(:id).where(:id => 2).to_sql + ") ")

[1m[36mCandidat Load (1.0ms)[0m  [1mSELECT "candidats".* FROM "candidats" WHERE (id = (SELECT id FROM "sessiontests" WHERE "sessiontests"."id" = 6) / (SELECT id FROM "sessiontests" WHERE "sessiontests"."id" = 2) )[0m

But I find this ugly. I will try to get these subqueries working in a more dynamic way. I mean replace the integer values by columns name.

  • 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-13T05:15:27+00:00Added an answer on June 13, 2026 at 5:15 am

    I don’t have anymore the exact answer to this question, because i do not work in the same enterprise anymore. But the solution to this problem, was to use a group_by clause. So the request became really easy.

    With a group_by, i was able to manipulate, category or a technology with ease.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I am doing a simple coin flipping experiment for class that involves flipping 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.