如何:隐藏Feed?

时间:2015-04-23 21:25:33

标签: ruby-on-rails ruby model-view-controller scope feed

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

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&amp;中的:name在其个人资料中看到此评估的@valuations = @user.valuations.publish。在valuations.rb。{/ p>中scope :publish, ->{ where(:conceal => false) }

我们怎样才能隐瞒活动饲料的估值?以下是与活动相同的估值:

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

1 个答案:

答案 0 :(得分:1)

在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