问题
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">
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
回答1:
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
来源:https://stackoverflow.com/questions/29834607/how-to-conceal-from-feed