Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

select extra values not available in model #7600

Closed
nijikon opened this Issue · 8 comments

6 participants

@nijikon

Hello,

I want to run this.

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

j = Array.new
j << QueryDef.strategy()

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

km = KeywordMap.select(s).joins(:ad_group => :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?

@rafaelfranca

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

@tenderlove could you confirm?

@nijikon

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
     end

     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)
     end

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

@al2o3cr

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:

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/finder_methods.rb#L205

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

@nijikon

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?

@ugisozols

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

Yes.

@senny
Owner

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

@steveklabnik
Collaborator

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