Rails ".pluck" on joined tables with columns of the same name returns a single value, and then nil

Experiment has_many :features Feature belongs_to :experiment Experiment.where("experiments.id=1") .joins(:features) .pluck("features.id", "experiments.id") 

I expect this to return the identifier of each function and the experiment identifier.

 [ [1, 1], [2, 1], [3, 1], # .... ] 

Instead, the experiment id is returned, and then nil

 [ [1, nil], [1, nil], [1, nil], # .... ] 

This is strange in three ways:

  • although this is an inner join, only the Experiment returns. I can cut columns from functions (features.name).
  • everything is fine until the column name is repeated.
  • first the last plucked column is displayed, as if the first column was overwritten. Toggle pop order returns the return value.

This seems like a mistake, but maybe I'm doing something wrong. Any tips?

SQL output:

 SELECT features.id, experiments.id FROM "experiments" INNER JOIN "features" ON "features"."experiment_id" = "experiments"."id" WHERE (experiments.id=1) 

Note. This is a simplified question related to a query similar to:

 Experiment.where("experiments.id=1") .joins(feature: { child2: { :child3 } }) .pluck("feature.id, child3.id") 
+7
ruby sql ruby-on-rails
source share
2 answers

This is a bit complicated. Since there INNER JOIN request produces only one id in this case. You can form a request in another way:

 Feature.joins(:experiment) .where(features: { experiment_id: 1 }) .pluck(:id, :experiment_id) 

Or:

 Feature.joins(:experiment) .where(experiments: { id: 1 }) .pluck(:id, :experiment_id) 
+6
source share

After publishing github on rails, I found that my specific problem was resolved in the latest version of rails (4.1.6).

https://github.com/rails/rails/issues/17049

+3
source share

All Articles