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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:18:35+00:00 2026-05-24T17:18:35+00:00

I’ve got a model (a Feature ) that can have many Assets . These

  • 0

I’ve got a model (a Feature) that can have many Assets. These Assets each have an issue_date. I’m struggling with what seems like a simple ActiveRecord query to find all Features and their Assets with an issue_date of tomorrow, regardless of if there are Assets or not — preferably with one query.

Here’s my query right now.

Feature.includes(:assets).where(:assets => { :issue_date => Date.tomorrow })

Unfortunately, this returns only the Features that have Assets with an issue_date of tomorrow. Even stranger, the generated SQL looks like this (tomorrow’s obviously the 19th).

SELECT `features`.* FROM `features`  WHERE `assets`.`issue_date` = '2011-08-19'

Shouldn’t this have an LEFT JOIN in there somewhere? That’s the sort of thing I’m going for. Using joins instead of includes does an INNER JOIN, but that’s not what I want. Strangely enough, it seems like I’m getting an INNER JOIN-type of behavior. When I run that includes query above, the actual SQL that’s spit out looks something like this…

SELECT `features`.`id` AS t0_r0, `features`.`property_id` AS t0_r1,
    // every other column from features truncated for sanity
    `assets`.`feature_id` AS t1_r1, `assets`.`asset_type` AS t1_r2,
    // all other asset columns truncated for sanity
FROM `features`
LEFT OUTER JOIN `assets` ON `assets`.`feature_id` = `features`.`id`
WHERE `assets`.`issue_date` = '2011-08-19'

Which looks like it should work right but it doesn’t. I get only the Features that have Assets with an issue_date of tomorrow. Any idea what I’m doing wrong?

I’ve tried the older, Rails v2 way of doing it…

Feature.find(:all,
             :include => :assets,
             :conditions => ['assets.issue_date = ?', Date.tomorrow])

Which gives me the same results. There’s one Feature I know that doesn’t have any Assets for tomorrow, and it’s not in that list.

I’ve also poked around and found similar questions, but I couldn’t seem to find one that explained this opposite behavior I’m seeing.

Edit: I’m so close. This gets me all the Feature objects.

Feature.joins("LEFT OUTER JOIN assets on assets.feature_id = feature.id AND asset.issue_date = #{Date.tomorrow}")

It does not, however, get me the matching Assets bundled into the object. With feature as a returned item in the query, feature.assets makes another call to the database, which I don’t want. I want feature.assets to return only those I’ve specified in that LEFT OUTER JOIN call. What else do I need to do to my query?

  • 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-24T17:18:36+00:00Added an answer on May 24, 2026 at 5:18 pm

    I thought this would get me what I needed, but it doesn’t. Calling feature.assets (with feature as an item returned in my query) does another query to look for all assets related to that feature.

    Feature.joins("LEFT OUTER JOIN assets on assets.feature_id = feature.id AND asset.issue_date = #{Date.tomorrow}")
    

    So here’s what does work. Seems a little cleaner, too. My Feature model already has a has_many :assets set on it. I’ve set up another association with has_many :tomorrows_assets that points to Assets, but with a condition on it. Then, when I ask for Feature.all or Feature.name_of_scope, I can specify .includes(:tomorrows_assets). Winner winner, chicken dinner.

    has_many :tomorrows_assets,
      :class_name => "Asset",
      :readonly => true, 
      :conditions => "issue_date = '#{Date.tomorrow.to_s}'"
    

    I can successfully query Features and get just what I need included with it, only if it matches the specified criteria (and I’ve set :readonly because I know I’ll never want to edit Assets like this). Here’s an IRB session that shows the magic.

    features = Feature.includes(:tomorrows_assets)
    feature1 = features.find_all{ |f| f.name == 'This Feature Has Assets' }.first
    feature1.tomorrows_assets
    => [#<Asset id:1>, #<Asset id:2>]
    
    feature2 = features.find_all{ |f| f.name == 'This One Does Not' }.first
    feature2.tomorrows_assets
    => []
    

    And all in only two SQL queries.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.