Skip to content

select extra values not available in model #7600

nijikon opened this Issue Sep 11, 2012 · 8 comments

6 participants

nijikon commented Sep 11, 2012


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?

Ruby on Rails member

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

@tenderlove could you confirm?

nijikon commented Sep 11, 2012

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.

al2o3cr commented Sep 11, 2012

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.

nijikon commented Sep 12, 2012

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?


Ruby on Rails member
senny commented Mar 7, 2013

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

Ruby on Rails member

@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.