Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


select extra values not available in model #7600

nijikon opened this Issue · 8 comments

6 participants



I want to run this.

s =
s << 'strategy.id_strategy AS strategy_id'

j =
j << QueryDef.strategy()

w =
w << 'campaign.managed = 1'
w << 'strategy.id_strategy IS NOT NULL'

km = => :campaign).joins(j).includes(:keyword_update, :ad_group => :campaign).where(w.join(' AND ')).first

Desired output would be a KeywordMap object with strategy_id available, but is not happening.

(rdb:1) km.strategy_id
NoMethodError Exception: undefined method `strategy_id' for #<KeywordMap:0x007f9a8f417e28>

My question here is, should the field be available on KeywordMap model or not?


I think this is already fixed on master but it not trivial to backport.

@tenderlove could you confirm?


I checked on master, it does not work on master either.

First of all this patch needs to be introduce, so select values would be included in the query.

diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 84aaa39..d9ee1dd 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -202,7 +202,7 @@ module ActiveRecord

     def construct_relation_for_association_find(join_dependency)
-      relation = except(:includes, :eager_load, :preload, :select).select(join_dependency.columns)
+      relation = except(:includes, :eager_load, :preload).select(join_dependency.columns)
       apply_join_dependency(relation, join_dependency)

I want to fix this, i just need some guideline in which file should test for this be included.


This is another instance of the "includes trash selects" issue that I've seen pop up before - the generated SQL for includes overrides what's specified in select. The relevant code is in construct_relation_for_association_find, called by find_with_associations, called by Relation's exec_queries:

In principle, it should be possible to extend the columns method and associated plumbing on JoinDependency to add the extra columns.


Agree, I saw like 5 tickets lately regarding select and includes issues. I will try to wrap up them all and fix all the issues that are there.

Just a quick question, first fix it on master and then backport to 3.2-stable, yes?


Just a quick question, first fix it on master and then backport to 3.2-stable, yes?



@nijikon did you have time to work on that PR?


@nijikon , it's been a really long time since we've heard from you, and this is sort of in the 'enhancement' category. So I'm giving it a close.

If anyone wants to get this behavior into Rails, please send a pull request, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.