问题描述
有许多评估,活动和用户.每个表都有这一行:
There are many valuations, activities, and users. Each table has this line:
t.boolean "conceal", default: false
提交估值时,可以将其设置为真实:
When submitting a valuation it can be made true:
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
在其个人资料上看到此评估的:name
. scope :publish, ->{ where(:conceal => false) }
在valuation.rb中.
This prevents other user's from seeing this valuation's :name
on his profile via @valuations = @user.valuations.publish
in the users_controller & scope :publish, ->{ where(:conceal => false) }
in valuations.rb.
我们还如何在活动供稿中隐藏此评估?这是一项与活动相同的估价:
How can we also conceal this valuation on the activities feed? Here is this same valuation found as an 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>
您在这里看到它是怎么错的?我们如何才能做到这一点?
You see how it is false here? How can we make it true?
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模型中使用布尔值.只需创建一个从评估记录中获取隐蔽值的吸气剂方法即可.
You don't actually need the boolean in your Activity model. Just create a getter method that gets the conceal value from the Valuation record.
class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :trackable, polymorphic: true
scope :publish, ->{ where(:conceal => false) }
def conceal
trackable.conceal
end
end
这篇关于如何从Feed隐藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!