Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从中的联接表中访问数据有多个:通过,防止了额外的数据加载_Mysql_Ruby On Rails 4_Activerecord - Fatal编程技术网

Mysql 从中的联接表中访问数据有多个:通过,防止了额外的数据加载

Mysql 从中的联接表中访问数据有多个:通过,防止了额外的数据加载,mysql,ruby-on-rails-4,activerecord,Mysql,Ruby On Rails 4,Activerecord,轨道4.1.7 我有三种型号 # Report class Report < ActiveRecord::Base has_many :computed_values, dependent: :destroy has_many :settlements, through: :computed_values end # ComputedValue class ComputedValue < ActiveRecord::Base belongs_to :report b

轨道4.1.7

我有三种型号

# Report
class Report < ActiveRecord::Base
  has_many :computed_values, dependent: :destroy
  has_many :settlements, through: :computed_values
end

# ComputedValue
class ComputedValue < ActiveRecord::Base
  belongs_to :report
  belongs_to :settlement
end

# Settlement
class Settlement < ActiveRecord::Base
  has_many :computed_values
  has_many :reports, through: :computed_values
end
#报告
类报告
ComputedValue
具有一个属性
distance

我想得到这样的建筑工程:
Report.first.resolutions.first.distance
这是
ComputedValue.find(Report\u id:Report.first.id,consolution\u id:Report.first.resolutions.first.id)。distance

有什么优雅而快速的方法可以让它工作吗

当我调用
Report.first.resolutions.first
时,Rails已经从联接表
中加载了第一个报告、第一个结算和记录的记录。

如何防止从
计算的\u值中再次加载以查找值并使用已加载记录中的数据?

好的,我找到了一个解决方案

# Report
has_many :settlements, -> {select("settlements.*, computed_values.distance AS distance")},
          through: :computed_values
在那之后,
报告。第一。定居点。第一。距离
工作得非常好