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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:05:12+00:00 2026-05-14T01:05:12+00:00

I want to add additional criteria to the LEFT OUTER JOIN generated by the

  • 0

I want to add additional criteria to the LEFT OUTER JOIN generated by the :include option in ActiveRecord finder.

class Post
  has_many :comments
end

class Comment
  belongs_to :post
  has_many   :comment_votes
end

class CommentVote
  belongs_to :comment
end

Now lets say I want to find last 10 posts with their associated comments and the up votes.

Post.find.all(:limit => 10, :order => "created_at DESC",
    :include => [{:comments => :comment_votes])

I cant add the condition to check for up votes as it will ignore the posts without the up votes. So the condition has to go to the ON clause of the JOIN generated for the comment_votes. I am wishing for a syntax such as:

Post.find.all(:limit => 10, :order => "created_at DESC",
    :include => [{:comments => [:comment_votes, :on => "comment_votes.vote > 0"])

Have you faced such problems before? Did you managed to solve the problem using the current finder? I hope to hear some interesting ideas from the community.

PS: I can write a join SQL to get the expected result and stitch the results together. I want to make sure there is no other alternative before going down that path.

  • 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-14T01:05:12+00:00Added an answer on May 14, 2026 at 1:05 am

    If I’m following your question correctly, something like this may work:

    class Comment
      belongs_to :post
      has_many   :comment_votes
      has_many   :up_votes, :class_name => "CommentVote", :conditions => "vote > 0"
    end
    

    Then your finder would be:

    Post.find.all(:limit => 10, :order => "created_at DESC",
    :include => {:comments => :up_votes})
    

    Note: I didn’t test this.

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

Sidebar

Related Questions

I want to add additional stuff to the parent class. I can't modify the
I want to add an additional roadblock in my application to prevent automating it
I want to add an additional value into an array before passing it to
I have dbml with single table users i want add partial class for User
We have completed 95% of our application. We want to add additional argument mode=1
In Teamcity I'm kicking off an Ant Build. I want to add additional jars
I have an MP3 file uploader. I want to add an additional audio track
I have a linq statement that I want to add an additional where clause
I want to add same additional fields to my form, but I don't want
I have an array with string values. I want to add additional text before

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.