如何:从 Feed 中隐藏?

How to :conceal from Feed?

估值多,活动多,用户多。每个 table 都有这一行:

t.boolean  "conceal",        default: false

提交估价时,可以实现:

pry(main)> Valuation.find(16)
  Valuation Load (0.1ms)  SELECT  "valuations".* FROM "valuations" WHERE "valuations"."id" = ? LIMIT 1  [["id", 16]]
=> #<Valuation:0x007fbbee41cf60
 id: 16,
 conceal: true,
 user_id: 1,
 created_at: Thu, 23 Apr 2015 20:24:09 UTC +00:00,
 updated_at: Thu, 23 Apr 2015 20:24:09 UTC +00:00,
 likes: nil,
 name: "CONCEAL NEW">

这可以防止其他用户通过 users_controller 中的 @valuations = @user.valuations.publish 和 valuations.rb 中的 scope :publish, ->{ where(:conceal => false) } 在他的个人资料中看到此估值的 :name

我们如何在活动提要中也隐藏此估值?这是与 activity:

相同的估值

Activity.find(24)
  Activity Load (0.1ms)  SELECT  "activities".* FROM "activities" WHERE "activities"."id" = ? LIMIT 1  [["id", 24]]
=> #<Activity:0x007fbbebd26438
 id: 24,
 user_id: 1,
 action: "create",
 test: nil,
 trackable_id: 16,
 trackable_type: "Valuation",
 created_at: Thu, 23 Apr 2015 20:24:09 UTC +00:00,
 updated_at: Thu, 23 Apr 2015 20:24:09 UTC +00:00,
 conceal: false>

你看到这里是假的了吗?我们怎样才能让它成为现实?

class Activity < ActiveRecord::Base
  belongs_to :user
  belongs_to :trackable, polymorphic: true
    scope :publish, ->{ where(:conceal => false) }
end


class ActivitiesController < ApplicationController
    def index
        @activities = Activity.publish.order("created_at desc").where(user_id: current_user.following_ids)
    end
end

您实际上不需要 Activity 模型中的布尔值。只需创建一个 getter 方法,从估价记录中获取隐藏值。

class Activity < ActiveRecord::Base
  belongs_to :user
  belongs_to :trackable, polymorphic: true
    scope :publish, ->{ where(:conceal => false) }

  def conceal
    trackable.conceal
  end
end