2

我在我的应用程序中使用了两个 gem,public_activity 和acts_as_follower。我已经很好地设置了acts_as_follower,用户可以在其中关注另一个用户以及乐队。

问题是从您关注的对象中获取活动的信息。使用公共活动,我有一个使用推荐方法的全局提要:

@activities = PublicActivity::Activity.order("created_at desc")

这很好用。但是,我还希望有以下提要,这将需要以下内容:

@activities = PublicActivity::Activity.order("created_at desc").where(owner_id: current_user.all_following, owner_type: ????????)

如果不指定 owner_type,则仅使用 owner_id 创建提要,这是一个问题,因为用户和乐队可以具有相同的 id。在这种情况下,我该如何指定所有者类型?

4

2 回答 2

1

对于用户:

@activities = PublicActivity::Activity.where(owner_id: current_user.follows_by_type('User'), owner_type: 'User').order("created_at desc")

对于乐队:

@activities = PublicActivity::Activity.where(owner_id: current_user.follows_by_type('Band'), owner_type: 'Band').order("created_at desc")

对于混合物:

@activities = PublicActivity::Activity.where("(owner_id IN (?) AND owner_type = 'User') or (owner_id IN (?) AND owner_type = 'Band')", current_user.follows_by_type('User'), current_user.follows_by_type('Band')).order("created_at desc")
于 2013-05-10T15:07:00.203 回答
0

Owen, here you use so called polymorphic associations try to figure out how they work. It is about just putting into the database not only ids, but the types of objects.

于 2013-03-21T12:04:05.353 回答