gpt4 book ai didi

ruby-on-rails - Rails 3 Eager Loading with conditions - 如何访问急切加载的数据?

转载 作者:行者123 更新时间:2023-12-01 06:44:48 25 4
gpt4 key购买 nike

我的应用程序中有很多情况,其中用户在与另一个对象(“组”)的关联中不超过一个对象(例如,“描述”)。

例如:

class User < ActiveRecord::Base
has_many :descriptions
has_many :groups

class Group < ActiveRecord::Base
has_many :users
has_many :descriptions

class Description < ActiveRecord::Base
belongs_to :user
belongs_to :group

如果我想呈现某个组中的所有用户并包含他们的相关描述,我可以这样做:
#users model
def description_for(group_id)
descriptions.find_by_group_id(group_id)
end

#view
@group.users.each do |user|
user.name
user.description_for(@group.id).content

但这会产生大量的描述查询。我试过使用连接:
#controller
@group = Group.find(params[:id], :joins => [{:users => :descriptions}], :conditions => ["descriptions.group_id = ?", params[:id]])

但由于我仍在调用 user.description_for(@group.id) 它对页面加载没有帮助。

更新:示例生成的 SQL
Rendered users/_title.html.haml (1.6ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 37 LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 7 LIMIT 1
CACHE (0.0ms) SELECT "groups".* FROM "groups" WHERE "groups"."id" = 28 LIMIT 1
Description Load (0.1ms) SELECT "descriptions".* FROM "descriptions" WHERE "descriptions"."target_id" = 7 AND "descriptions"."group_id" = 28 LIMIT 1
CACHE (0.0ms) SELECT "descriptions".* FROM "descriptions" WHERE "descriptions"."target_id" = 7 AND "descriptions"."group_id" = 28 LIMIT 1
Rendered users/_title.html.haml (1.7ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 37 LIMIT 1
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 51 LIMIT 1
CACHE (0.0ms) SELECT "groups".* FROM "groups" WHERE "groups"."id" = 28 LIMIT 1
Description Load (0.1ms) SELECT "descriptions".* FROM "descriptions" WHERE "descriptions"."target_id" = 51 AND "descriptions"."group_id" = 28 LIMIT 1
CACHE (0.0ms) SELECT "descriptions".* FROM "descriptions" WHERE "descriptions"."target_id" = 51 AND "descriptions"."group_id" = 28 LIMIT 1
Rendered users/_title.html.haml (1.8ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 37 LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
CACHE (0.0ms) SELECT "groups".* FROM "groups" WHERE "groups"."id" = 28 LIMIT 1
Description Load (0.1ms) SELECT "descriptions".* FROM "descriptions" WHERE "descriptions"."target_id" = 5 AND "descriptions"."group_id" = 28 LIMIT 1
CACHE (0.0ms) SELECT "descriptions".* FROM "descriptions" WHERE "descriptions"."target_id" = 5 AND "descriptions"."group_id" = 28 LIMIT 1
Rendered users/_title.html.haml (1.7ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 37 LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 52 LIMIT 1
CACHE (0.0ms) SELECT "groups".* FROM "groups" WHERE "groups"."id" = 28 LIMIT 1
Description Load (0.2ms) SELECT "descriptions".* FROM "descriptions" WHERE "descriptions"."target_id" = 52 AND "descriptions"."group_id" = 28 LIMIT 1
CACHE (0.0ms) SELECT "descriptions".* FROM "descriptions" WHERE "descriptions"."target_id" = 52 AND "descriptions"."group_id" = 28 LIMIT 1
Rendered users/_title.html.haml (1.7ms)

最佳答案

是的,我认为实际上您不需要 rails 3 中的 joins 子句。如果您使用 include 和 where,Arel 将为您完成艰苦的工作。

我已经使用具有相同底层关联排列的模型对此进行了测试(尽管使用了与您的模型(和属性)不同的模型集),我认为这应该可行:

在模型/user.rb 中:

scope :with_group_and descriptions, lambda { |group_id| includes(:groups, :descriptions).where(:groups => { :id => group_id }, :descriptions => { :group_id => group_id }) }

然后在您的 Controller 中调用:
@users = User.with_group_and_descriptions(params[:id])

最后在 View 中,您可以执行以下操作:
@users.each do |user|
user.name
user.descriptions.each do |desc|
desc.content
# or
@users.each do |user|
user.name
user.descriptions[0].content

如果我的想法正确,那么这应该只进行 2 次分贝调用。一个获取 user_ids 列表,第二个获取用户、组和描述数据,即使您正在调用用户对象的描述方法,该方法通常包含所有描述(不仅仅是特定组的描述) ),因为您已经填充了关联 rails ,所以当您调用 user.descriptions 时,不会再次抓取所有关联。 ,相反,它只会列出您使用 descriptions.group_id where 子句从数据库中提取的那些。调用 user.descriptions(true)但是将强制重新加载导致它返回数组 的描述。全部 用户的描述关联。

关于ruby-on-rails - Rails 3 Eager Loading with conditions - 如何访问急切加载的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6822267/

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