2016-02-29 90 views
1

除了我在用户和艺术家模型之间设置的多态活动跟踪之外,我正在使用acts_as_follower gem。我现在试图在用户的节目视图中显示跟随艺术家的所有活动。起初,我以为我可以简单地使用宝石的all_follows方法创造艺术家的一个实例变量:获取相关对象的活动

current_user.all_follows 
=> #<ActiveRecord::AssociationRelation [#<Follow id: 8, followable_id: 1, followable_type: "Artist", follower_id: 1, follower_type: "User", blocked: false, created_at: "2016-02-29 12:56:31", updated_at: "2016-02-29 12:56:31">]> 

正如你可以看到它返回Follow对象(S)主场迎战Artist对象我期待找回。我不得不对Activity类执行SQL查询另外一个设想:

Activity.where(followable_id: current_user.follows.pluck(&:id)) 

这给我的错误:

SQLite3::SQLException: no such column: activities.followable_id: SELECT "activities".* FROM "activities" WHERE "activities"."followable_id" IN (8, 1, 'Artist', 1, 'User', 'f', '2016-02-29 12:56:31.974244', '2016-02-29 12:56:31.974244') 
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: activities.followable_id: SELECT "activities".* FROM "activities" WHERE "activities"."followable_id" IN (8, 1, 'Artist', 1, 'User', 'f', '2016-02-29 12:56:31.974244', '2016-02-29 12:56:31.974244') 

什么是检索这些记录的最好方法?

class Artist < ActiveRecord::Base 
    has_many :activities, as: :owner, dependent: :destroy 
    acts_as_followable 
end 

class User < ActiveRecord::Base 
    acts_as_follower 
end 

class Activity < ActiveRecord::Base 
    belongs_to :trackable, polymorphic: true 
    belongs_to :owner, polymorphic: true 
end 

class Follow < ActiveRecord::Base 
    extend ActsAsFollower::FollowerLib 
    extend ActsAsFollower::FollowScopes 

    belongs_to :followable, polymorphic: true 
    belongs_to :follower, polymorphic: true 
end 

class UsersController < ApplicationController 
    def show 
    @artist_activity = Activity.where(followable_id: current_user.follows.pluck(&:id)) 
    end 
end 
+0

你能发布你的模式.. –

+0

在Activity表中没有名为'followable_id'的列,所以你不能这样做,并且'activity'和'fol​​low'模型之间没有关系 –

+0

你的环境是什么(ruby和rails版本)?我要复制你的设置,以便我可以和你一起工作。 – jvillian

回答

0

这似乎为我工作:

Activity 
    .where(owner_type: 'Artist') 
    .where(:owner_id => current_user.all_follows 
          .where(followable_type: 'Artist') 
          .map(&:followable) 
          .map{|artist| artist.id} 
    ) 

这可能是更好的:

Activity.where(owner: current_user.all_follows 
            .where(followable_type: 'Artist') 
            .map(&:followable) 
     ) 

但我没有测试它。

因此,该位:

current_user.all_follows 
       .where(followable_type: 'Artist') 
       .map(&:followable) 

创建借助于.map(&followable)位的对象的数组。在这种情况下,由于.where(followable_type: 'Artist'),它恰好是Artist对象的数组。

Activity规定所有者是多态的。所以,AR在传递一系列对象时足够智能,可以在owner_typeowner_id上匹配 - 所以您不必像第一个例子那样明确。

随着排序:

Activity.where(owner: current_user.all_follows 
            .where(followable_type: 'Artist') 
            .map(&:followable) 
     ).order(created_at: :desc) 
+0

谢谢。因为我正在收集潜在的众多艺术家@ @ artist'代表什么? –

+0

对不起,我以为你想要一个**特定**艺术家的活动。将更新答案。 – jvillian

+0

是的。基本上我想收集个人关注的所有艺术家的活动。 –

0

原本是在正确的轨道上,而是应该由所有者(即艺术家),并从那里已经聚集查询和采摘其followable_id

class UsersController < ApplicationController 
    def show  
    @artist_activity = Activity.order("created_at desc").where(owner: current_user.all_follows.pluck(&:followable_id)) 
    end 
end 
+0

你的系统会不会演变到一个用户可以追寻艺术家以外的地方的地方? – jvillian

+0

是的。可能还有其他用户在路上。你为什么要问? –

+0

首先,让我说我喜欢你所做的,我自己也学到了很多东西。所以,这完全是这个精神。现在,我可能会误认为,但假设您有:id为1的艺术家,id为2的艺术家,id为1的组以及id为2的组。所有人都有活动。 current_user跟随ID为1的艺术家和ID为2的组。我认为你的方法将为**所有艺术家**和**所有组**包括那些艺术家和组合当前用户不关注。但是,我可能是错的。 – jvillian