gpt4 book ai didi

ruby-on-rails - Rails 急切加载似乎查询错误

转载 作者:行者123 更新时间:2023-12-01 11:05:51 25 4
gpt4 key购买 nike

我正在尝试在我的 Rails 3 应用程序中进行预加载。我已将其缩小为一个非常基本的示例,它没有生成我期望的一个查询,而是生成了 4 个。

首先,这是我的模型的简单分解。

class Profile < ActiveRecord::Base
belongs_to :gender

def to_param
self.name
end
end

class Gender < ActiveRecord::Base
has_many :profiles, :dependent => :nullify
end

然后我有一个 ProfilesController::show 操作,我在哪里查询模型。

def ProfilesController < ApplicationController
before_filter :find_profile, :only => [:show]

def show
end

private

def find_profile
@profile = Profile.find_by_username(params[:id], :include => :gender)
raise ActiveRecord::RecordNotFound, "Page not found" unless @profile
end
end

当我查看它生成的查询时,它显示以下内容:

SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`username` = 'matt' LIMIT 1
SELECT `genders`.* FROM `genders` WHERE (`genders`.`id` = 1)

我希望看到的是一个查询:

SELECT `profiles`.*, `genders`.* FROM `profiles` LEFT JOIN `genders` ON `profiles`.gender_id = `genders`.id WHERE `profiles`.`username` = 'matt' LIMIT 1

有人知道我在这里做错了什么吗?我在预加载中发现的所有内容都表明这应该可行。

编辑:按照 sled 的建议尝试加入后,我仍然看到相同的结果。

代码:

@profile = Profile.joins(:gender).where(:username => params[:id]).limit(1).first

查询:

SELECT `profiles`.* FROM `profiles` INNER JOIN `genders` ON `genders`.`id` = `profiles`.`gender_id` WHERE `profiles`.`username` = 'matt' LIMIT 1

同样,您可以看到没有检索到 genders 数据,因此正在对 genders 进行第二次查询。

我什至尝试添加一个select,但无济于事:

@profile = Profile.joins(:gender).select('profiles.*, genders.*').where(:username => params[:id]).limit(1).first

正确的结果是:

SELECT profiles.*, genders.* FROM `profiles` INNER JOIN `genders` ON `genders`.`id` = `profiles`.`gender_id` WHERE `profiles`.`username` = 'matt' LIMIT 1

...但在访问 @profile.gender 的属性时,它仍然对 genders 执行了第二次查询。

编辑 2: 我还尝试创建一个包含 selectjoins 的范围,以获得我需要的所有字段,(类似于 sled 演示的自定义左连接方法)。它看起来像这样:

class Profile < ActiveRecord::Base
# ...
ALL_ATTRIBUTES = [:photo, :city, :gender, :relationship_status, :physique, :children,
:diet, :drink, :smoke, :drug, :education, :income, :job, :politic, :religion, :zodiac]

scope :with_attributes,
select((ALL_ATTRIBUTES.collect { |a| "`#{reflect_on_association(a).table_name}`.*" } + ["`#{table_name}`.*"]).join(', ')).
joins(ALL_ATTRIBUTES.collect { |a|
assoc = reflect_on_association(a)
"LEFT JOIN `#{assoc.table_name}` ON `#{table_name}`.#{assoc.primary_key_name} = `#{assoc.table_name}`.#{assoc.active_record_primary_key}"
}.join(' '))
# ...
end

这会生成以下查询,看起来是正确的:

SELECT `photos`.*, `cities`.*, `profile_genders`.*, `profile_relationship_statuses`.*, `profile_physiques`.*, `profile_children`.*, `profile_diets`.*, `profile_drinks`.*, `profile_smokes`.*, `profile_drugs`.*, `profile_educations`.*, `profile_incomes`.*, `profile_jobs`.*, `profile_politics`.*, `profile_religions`.*, `profile_zodiacs`.*, `profiles`.* FROM `profiles` LEFT JOIN `photos` ON `profiles`.photo_id = `photos`.id LEFT JOIN `cities` ON `profiles`.city_id = `cities`.id LEFT JOIN `profile_genders` ON `profiles`.gender_id = `profile_genders`.id LEFT JOIN `profile_relationship_statuses` ON `profiles`.relationship_status_id = `profile_relationship_statuses`.id LEFT JOIN `profile_physiques` ON `profiles`.physique_id = `profile_physiques`.id LEFT JOIN `profile_children` ON `profiles`.children_id = `profile_children`.id LEFT JOIN `profile_diets` ON `profiles`.diet_id = `profile_diets`.id LEFT JOIN `profile_drinks` ON `profiles`.drink_id = `profile_drinks`.id LEFT JOIN `profile_smokes` ON `profiles`.smoke_id = `profile_smokes`.id LEFT JOIN `profile_drugs` ON `profiles`.drug_id = `profile_drugs`.id LEFT JOIN `profile_educations` ON `profiles`.education_id = `profile_educations`.id LEFT JOIN `profile_incomes` ON `profiles`.income_id = `profile_incomes`.id LEFT JOIN `profile_jobs` ON `profiles`.job_id = `profile_jobs`.id LEFT JOIN `profile_politics` ON `profiles`.politic_id = `profile_politics`.id LEFT JOIN `profile_religions` ON `profiles`.religion_id = `profile_religions`.id LEFT JOIN `profile_zodiacs` ON `profiles`.zodiac_id = `profile_zodiacs`.id WHERE `profiles`.`username` = 'matt' LIMIT 1

不幸的是,对关系属性(例如:@profile.gender.name)的调用似乎没有使用原始 SELECT 中返回的数据。相反,我看到第一个查询之后出现大量查询:

Profile::Gender Load (0.2ms)  SELECT `profile_genders`.* FROM `profile_genders` WHERE `profile_genders`.`id` = 1 LIMIT 1
Profile::Gender Load (0.4ms) SELECT `profile_genders`.* FROM `profile_genders` INNER JOIN `profile_attractions` ON `profile_genders`.id = `profile_attractions`.gender_id WHERE ((`profile_attractions`.profile_id = 2))
City Load (0.4ms) SELECT `cities`.* FROM `cities` WHERE `cities`.`id` = 1 LIMIT 1
Country Load (0.3ms) SELECT `countries`.* FROM `countries` WHERE `countries`.`id` = 228 ORDER BY FIELD(code, 'US') DESC, name ASC LIMIT 1
Profile Load (0.4ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`id` = 2 LIMIT 1
Profile::Language Load (0.4ms) SELECT `profile_languages`.* FROM `profile_languages` INNER JOIN `profile_profiles_languages` ON `profile_languages`.id = `profile_profiles_languages`.language_id WHERE ((`profile_profiles_languages`.profile_id = 2))
SQL (0.3ms) SELECT COUNT(*) FROM `profile_ethnicities` INNER JOIN `profile_profiles_ethnicities` ON `profile_ethnicities`.id = `profile_profiles_ethnicities`.ethnicity_id WHERE ((`profile_profiles_ethnicities`.profile_id = 2))
Profile::Religion Load (0.5ms) SELECT `profile_religions`.* FROM `profile_religions` WHERE `profile_religions`.`id` = 2 LIMIT 1
Profile::Politic Load (0.2ms) SELECT `profile_politics`.* FROM `profile_politics` WHERE `profile_politics`.`id` = 3 LIMIT 1

最佳答案

您的示例很好,它将以两个查询结束,因为这就是 rails 中实现预加载的方式。如果您有许多关联记录,它会变得很方便。您可以阅读更多相关信息 here

您可能想要的是一个简单的连接:

@profile = Profile.joins(:gender).where(:username => params[:id])

编辑

如果配置文件由很多部分组成,这里有多种方法:

自定义左连接 - 也许有一个插件可以完成这项工作,否则我建议做类似的事情:

class Profile < ActiveRecord::Base

# .... code .....

def self.with_dependencies

attr_joins = []
attr_selects = []

attr_selects << "`profiles`.*"
attr_selects << "`genders`.*"
attr_selects << "`colors`.*"

attr_joins << "LEFT JOIN `genders` ON `gender`.`id` = `profiles`.gender_id"
attr_joins << "LEFT JOIN `colors` ON `colors`.`id` = `profiles`.color_id"

prep_model = select(attr_selects.join(','))

attr_joins.each do |c_join|
prep_model = prep_model.joins(c_join)
end

return prep_model
end

end

现在你可以这样做:

@profile = Profile.with_dependencies.where(:username => params[:id])

另一种解决方案是使用 :include => [:gender, :color] 它可能会有更多查询,但它是更清晰的“rails 方式”。如果您遇到性能问题,您可能需要重新考虑您的数据库架构,但您的负载真的这么重吗?

我的一个 friend 为这个简单的 1:n 关系(比如性别)写了一个很好的小解决方案,它叫做 simple_enum

关于ruby-on-rails - Rails 急切加载似乎查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6010268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com