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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:14:21+00:00 2026-05-26T03:14:21+00:00

Update: This may be something that just isn’t doable. See this TLDR: How do

  • 0

Update: This may be something that just isn’t doable. See this

TLDR: How do you conditionally load an association (say, only load the association for the current user) while also including records that don’t have that association at all?

Rails 3.1, here’s roughly the model I’m working with.

class User
  has_many :subscriptions
  has_many :collections, :through => :subscriptions
end

class Collection
  has_many :things
end

class Thing
  has_many :user_thing_states, :dependent => :destroy
  belongs_to :collection
end

class Subscription
  belongs_to :user
  belongs_to :collection
end

class UserThingState
  belongs_to :user
  belongs_to :thing
end

There exist many collections which have many things. Users subscribe to many collections and thereby they subscribe to many things. Users have a state with respect to things, but not necessarily, and are still subscribed to things even if they don’t happen to have a state for them. When a user subscribes to a collection and its associated things, a state is not generated for every single thing (which could be in the hundreds). Instead, states are generated when a user first interacts with a given thing. Now, the problem: I want to select all of the user’s subscribed things while loading the user’s state for each thing where the state exists.

Conceptually this isn’t that hard. For reference, the SQL that would get me the data needed for this is:

SELECT things.*, user_thing_states.* FROM things
# Next line gets me all things subscribed to
INNER JOIN subscriptions as subs ON things.collection_id = subs.collection_id AND subs.user_id = :user_id
# Next line pulls in the state data for the user
LEFT JOIN user_thing_states as uts ON things.id = uts.thing_id AND uqs.user_id = :user_id

I just don’t know how to piece it together in rails. What happens in the Thing class? Thing.includes(:user_thing_states) would load all states for all users and that looks like the only tool. I need something like this but am not sure how (or if it’s possible):

  class Thing
    has_many :user_thing_states
    delegates :some_state_property, :to => :state, :allow_nil => true

    def state
      # There should be only one user_thing_state if the include is correct, state method to access it.
      self.user_thing_states.first
    end
  end

I need something like:

Thing.includes(:user_question_states, **where 'user_question_state.user_id => :user_id**).by_collections(user.collections)

Then I can do

things = User.things_subscribed_to 
things.first.some_state_property # the property of the state loaded for the current user.
  • 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-26T03:14:21+00:00Added an answer on May 26, 2026 at 3:14 am

    Answering my own question twice… bit awkward but anyway.

    Rails doesn’t seem to let you specify additional conditions for an includes() statement. If it did, my previous answer would work – you could put an additional condition on the includes() statement that would let the where conditions work correctly. To solve this we’d need to get includes() to use something like the following SQL (Getting the ‘AND’ condition is the problem):

    LEFT JOIN user_thing_states as uts ON things.id = uts.thing_id AND uqs.user_id = :user_id
    

    I’m resorting to this for now which is a bit awful.

    class User
      ...
    
      def subscribed_things
        self.subscribed_things_with_state + self.subscribed_things_with_no_state
      end
    
      def subscribed_things_with_state
        self.things.includes(:user_thing_states).by_subscribed_collections(self).all
      end
    
      def subscribed_things_with_no_state
        Thing.with_no_state().by_subscribed_collections(self).all
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NEW INFO: Something I just noticed (because this isn't something I'd normally do), but
This may be an oxymoron, but how would one update a data entity in
Update: this question, including the title, was rephrased, see history for details I know
This may apply to other mobile operating environments than just Symbian... However, the issue
Ok this may be really simple but everything I try just seems to hit
Update: This turned into a blog post, with updated links and code, over at
Update: This does work, I was being stupid :( i have the following extension
Update: This is, as I was told, no principle Python related problem, but seems
UPDATE: this is a repost of How to make shell scripts robust to source
UPDATE: This question is out of date, but left for informational purposes. Original Question

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.