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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:17:42+00:00 2026-06-16T18:17:42+00:00

I am trying to apply two filters to a database query in Rails 3.

  • 0

I am trying to apply two filters to a database query in Rails 3. The first filter shows only media of type images. The second filter shows the highest saluted stories. On their own the filters work ok, but when I try to combine both filters, I get errors.

There are 3 tables involved. Stories, memories, and salutes. The salutes table keeps track of how many times someone ‘salutes’ a memory. Each story is composed of multiple memories. A story’s total salutes is the sum of the salutes of that story’s memories. I want to retrieve records of image-only stories in the order of highest to lowest salutes.

models/story.rb

def self.where_contains_image()
  joins(
    'INNER JOIN memories AS wci_memories ON wci_memories.story_id = stories.id'
  )
  .where(
    'wci_memories.media_type_cd = ?', Memory.image
  ).uniq
end

controllers/stories_controller.rb

if params[:filter_content] == 'image'
  stories = stories.where_contains_image
end

if (params[:filter_trends] == 'most_saluted')
  stories = stories.order("(SELECT COUNT(1) FROM salutes
    LEFT JOIN memories AS ms_memories ON salutes.content_id = ms_memories.id
    LEFT JOIN stories AS ms_stories ON ms_stories.id = ms_memories.story_id
    WHERE ms_stories.id = stories.id AND salutes.content_type = 'Memory')
    DESC");
end

On its own, when the ‘most_saluted’ param is set, the query works as expected. When both the ‘most_saluted’ param and the ‘image’ param are set, I get an error:

for SELECT DISTINCT, ORDER BY expressions must appear in select list

I understand what the error is, but I cannot figure out how to rewrite the queries so that it can return only images in the order of most saluted.

When I run this SQL query on the database, it returns the records I’m looking for. But I cannot figure out how to make rails return the same records. Furthermore, this query combines the two filters (only images and highest salutes). I want to keep them separate so that I can apply one filter individually, or both together.

SELECT DISTINCT stories.*, (SELECT COUNT(1) FROM salutes
    LEFT JOIN memories AS ms_memories ON salutes.content_id = ms_memories.id
    LEFT JOIN stories AS ms_stories ON ms_stories.id = ms_memories.story_id
    WHERE ms_stories.id = stories.id AND salutes.content_type = 'Memory') 
AS total_salutes FROM stories INNER JOIN memories AS wci_memories 
ON wci_memories.story_id = stories.id WHERE wci_memories.media_type_cd = 0
ORDER BY total_salutes DESC

Any thoughts on how I can resolve this?

  • 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-16T18:17:44+00:00Added an answer on June 16, 2026 at 6:17 pm

    You can use scope to achieve this, actually the Activerecord scope are the more cleaner/moduler way to chain conditions

    read here about scopes

    HTH

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

Sidebar

Related Questions

I want to apply two filters in my django queryset i am trying to
I am trying apply two background images for an entire table row. One background
I'm trying to apply two background images. One is a repeating block of colour
I'm trying to only apply the CSS :hover selector when two different classes are
I have two CIImages. I'm trying to apply a CGAffineTransformation to the top image,
I am trying to apply filters to employees to show them just on the
I am trying to apply smoothing filter to image . But I get this
I have two requiredfield validators in visual studio. Now I am trying to apply
I'm trying to build a content type that has two selectlist fields. My problem
I am trying to apply some CSS3 transformations on elements and there is two

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.