【问题标题】:Rails public_activity feed with polymorphic following具有多态跟随的 Rails public_activity 提要
【发布时间】:2013-03-08 03:00:42
【问题描述】:

我在我的应用程序中使用了两个 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。在这种情况下如何指定所有者类型?

【问题讨论】:

    标签: ruby-on-rails feed polymorphic-associations


    【解决方案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")
    

    【讨论】:

      【解决方案2】:

      欧文,你在这里使用所谓的polymorphic associations 尝试弄清楚它们是如何工作的。 它不仅仅是将 ids 放入数据库,还有对象的类型。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-28
        相关资源
        最近更新 更多