作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
艺术家有很多事件(基本上是用户之间交互的缓存):
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, :class_name => 'Artist', :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')
class ActivityStreamController < ApplicationController
def index
@activities = @artist.activities.includes([:link,:creator,:receiver]).order("id DESC").limit(30)
end
end
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)
activity.link.post
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)
最佳答案
TL;DR 我的解决方案使 artist.created_activities.includes(:link)
急切地加载你想要的一切
这是我的第一次尝试:https://github.com/JohnAmican/music
几点注意事项:
default_scope
,所以这不是最优的。 activities
关于艺术家;您必须引用 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, receiver_id: 2, creator_id: 1, link_id: 1, link_type: "Following", created_at: "2013-10-31 02:36:41", updated_at: "2013-10-31 02:36:41">, #<Activity id: 3, receiver_id: 2, creator_id: 1, link_id: 1, link_type: "Favorite", created_at: "2013-10-31 02:37:04", updated_at: "2013-10-31 02:37:04">]>
default_scope
告诉 Rails 你想做什么似乎是一个问题,而不是技术限制。
self
在该 block 内指的是关系。因此,您可以反射(reflection)并采取适当的行动:
class Activity < ActiveRecord::Base
belongs_to :creator, class_name: 'Artist', foreign_key: :creator_id, inverse_of: :created_activities
belongs_to :receiver, class_name: 'Artist', 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
正在 build 。这将允许您有条件地对其进行操作。
关于ruby-on-rails - 有没有办法急切加载多态关联的关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8652085/
我是一名优秀的程序员,十分优秀!