アプリで能力を定義するために CanCan を使用しています。
問題は、関連付けを通じてユーザーに属するモデルの能力を定義する方法です。
たとえば、キャンペーンにはaffiliate_idフィールドがあるため、これは機能します
can :manage, Campaign, :affiliate_id => affiliate.id
ただし、AffiliateshaveSubscriptionsとMessagesthrough Keywords. Keywordsに属しCampaignます。
すべての協会は正常に機能しています。Subscriptionsただし、 と のみに制限Messagesする方法がわかりませんcurrent_user。
これらは私の現在の関連付けです
# affiliate.rb
has_many :keywords, :through => :campaigns
has_many :campaigns
# campaign.rb
belongs_to :affiliate
has_many :keywords
has_many :subscriptions, :through => :keywords
has_many :messages, :through => :subscriptions
# keyword.rb
has_many :subscriptions
has_many :messages
belongs_to :campaign
# message.rb
belongs_to :user
belongs_to :campaign
belongs_to :subscription
belongs_to :keyword
# subscription.rb
belongs_to :campaign
belongs_to :affiliate
belongs_to :keyword
has_many :messages
また、(ボーナスの質問)、ユーザーが管理者の場合、すべてのオブジェクトを表示するにはどうすればよいですか? 私はこのようなものを持っていますが、すべてのオブジェクトを取得しているわけではありません。
if affiliate.status == "admin"
can :manage, :all
end