ruby-on-rails – 有没有办法急于加载多态关联的关联?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 有没有办法急于加载多态关联的关联?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
艺术家有很多活动(基本上是用户之间的互动缓存):
class Activity < ActiveRecord::Base

  belongs_to :receiver,:class_name => 'Artist',:foreign_key => :receiver_id #owns the stuff done "TO" him
  belongs_to :link,:polymorphic => true
  belongs_to :creator,:foreign_key => :creator_id #person who initiated the activity

end

例如:

Activity.create(:receiver_id => author_id,:creator_id => artist_id,:link_id => id,:link_type => 'ArtRating')

我想为每位艺术家创建一个活动流页面,包括不同类型的活动列表,ArtRatings(喜欢,不喜欢),收藏,关注等.

控制器看起来像这样:

class ActivityStreamController < ApplicationController
  def index
    @activities = @artist.activities.includes([:link,:creator,:receiver]).order("id DESC").limit(30)
  end
end

db调用正确地急切地加载多态链接对象:

SELECT "activities".* FROM "activities" WHERE (("activities"."receiver_id" = 6 OR "activities"."creator_id" = 6)) ORDER BY id DESC LIMIT 30
  ArtRating Load (0.5ms)  SELECT "art_ratings".* FROM "art_ratings" WHERE "art_ratings"."id" IN (137,136,133,130,126,125,114,104,103,95,85,80,73,64)
  SELECT "follows".* FROM "follows" WHERE "follows"."id" IN (14,10)
  SELECT "favorites".* FROM "favorites" WHERE "favorites"."id" IN (25,16,14)

但是当我显示每个ArtRating时,我还需要引用属于帖子的帖子标题.在视图中,如果我这样做:

activity.link.post

它为每个art_rating的帖子执行单独的DB调用.有没有办法急切加载帖子?

更新问题:

如果没有办法使用’includes’语法实现对帖子的热切加载,有没有办法自己手动执行急切加载查询并将其注入@activities对象?

我在DB日志中看到:

SELECT "art_ratings".* FROM "art_ratings" WHERE "art_ratings"."id" IN (137,64)

有没有办法可以从@activities对象访问这个id列表?如果是这样,我可以再做2个查询,1可以获得该列表中的art_ratings.post_id,另一个查询选择post_id列表中的所有帖子.然后以某种方式将’post’结果注入@activities,以便当我遍历集合时它可以作为activity.link.post使用.可能?

解决方法

TL; DR我的解决方案使artist.created_activities.includes(:link)急切加载你想要的一切

这是我的第一次尝试:https://github.com/JohnAmican/music

几点说明:

>我依赖于default_scope,所以这不是最佳选择.
>看起来你正在使用STI.我的解决方案没有.这意味着你不能简单地调用艺术家的活动;你必须引用created_activities或received_activities.可能有办法解决这个问题.如果我找到任何东西,我会更新.
>我改变了一些名字,因为这对我来说很困惑.

如果你进入控制台并执行created_activities.includes(:link),那么适当的东西会被加载:

irb(main):018:0> artist.created_activities.includes(:link)
  Activity Load (0.2ms)  SELECT "activities".* FROM "activities" WHERE "activities"."creator_id" = ?  [["creator_id",1]]
  Rating Load (0.3ms)  SELECT "ratings".* FROM "ratings" WHERE "ratings"."id" IN (1)
  RatingExplanation Load (0.3ms)  SELECT "rating_explanations".* FROM "rating_explanations" WHERE "rating_explanations"."rating_id" IN (1)
  Following Load (0.3ms)  SELECT "followings".* FROM "followings" WHERE "followings"."id" IN (1)
  Favorite Load (0.2ms)  SELECT "favorites".* FROM "favorites" WHERE "favorites"."id" IN (1)
=> #<ActiveRecord::Relation [#<Activity id: 1,receiver_id: 2,creator_id: 1,link_id: 1,link_type: "Rating",created_at: "2013-10-31 02:36:27",updated_at: "2013-10-31 02:36:27">,#<Activity id: 2,link_type: "Following",created_at: "2013-10-31 02:36:41",updated_at: "2013-10-31 02:36:41">,#<Activity id: 3,link_type: "Favorite",created_at: "2013-10-31 02:37:04",updated_at: "2013-10-31 02:37:04">]>

至少,这证明了Rails有能力做到这一点.绕过default_scope似乎是告诉Rails你想要做什么而不是技术限制的问题.

更新:

事实证明,当您将范围块传递给关联并调用该关联时,该块内的self将引用该关系.所以,你可以反思它并采取适当的行动:

class Activity < ActiveRecord::Base
  belongs_to :creator,class_name: 'Artist',foreign_key: :creator_id,inverse_of: :created_activities
  belongs_to :receiver,foreign_key: :receiver_id,inverse_of: :received_activities
  belongs_to :link,-> { self.reflections[:activity].active_record == Rating ? includes(:rating_explanation) : scoped },polymorphic: true
end

我更新了我的代码以反映(哈哈)这个.

这可以清理.例如,在访问活动链接时,您可能并不总是希望加载rating_explanations.有很多方法可以解决这个问题.如果你愿意,我可以发一个.

但是,我认为最重要的是,在关联的范围块中,您可以访问正在构建的ActiveRecord :: Relation.这将允许您有条件地做事.

原文链接:https://www.f2er.com/ruby/267910.html

猜你在找的Ruby相关文章