【问题标题】:How to avoid sendgrid overriding my classes? [duplicate]如何避免 sendgrid 覆盖我的课程? [复制]
【发布时间】:2019-04-08 14:12:13
【问题描述】:

我刚刚使用

将 Sendgrid 添加到我的应用程序中
gem 'sendgrid-ruby'

所以我开始在加载类时遇到问题。问题是,我已经有一个名为 client.rb 的模型

class Client < ApplicationRecord
  belongs_to :user

因此,有时 CanCanCan 或其他方法会被 Sendgrid 客户端类覆盖。例如这里:

if user.talent.present?
  # Talents can only read his own booking model
  can [:read, :accept, :check_in, :update], Booking, { talent: { user_id: user.id } }

  # Talents can only see his own connections
  can :read, Connection, Connection.where('user_1_id = :id or user_2_id  = :id', id: user.id) do |connection|
    (connection.user_1_id == user.id) || (connection.user_2_id == user.id)
  end

  can :create, Photo
  # can :read, Photo, user_id: user.id
  can :update, Photo, user_id: user.id
  can :delete, Photo, user_id: user.id

elsif user.client.present?
  can :create, Booking
  can [:read, :update], Booking, user_id: user.id
  #can :update, Booking, { talent: { user_id: user.id } }
  # Should we able to delete Bookings? or those created bookings get archive?

  # TODO: Really?
  # can :read, Agency
  # can :update, Client, user_id: user.id

在这个方法中user.client.present?破坏了应用程序,因为 CanCanCan 认为 Client 是 Sendgrid::Client 的一个实例,而不是我自己的模型

如何让我的应用始终知道“客户端”应该是我自己的类,而不是 Sendgrid 类?

我导入 sendgrid 的唯一要点是在我的邮件中:

require 'sendgrid-ruby'
include SendGrid

class AdminMailer < ApplicationMailer

【问题讨论】:

    标签: ruby-on-rails sendgrid


    【解决方案1】:
    include SendGrid
    

    是的,这就是你的问题所在。不要这样做。

    在你的辩护中,这条线确实存在于SendGrid's quick start snippets 中,但我不确定它的用途。看起来没用。 sendgrid 类的每次使用都是命名空间的。所以是的,只要删除它,你应该会很好。

    也看看这个:https://sendgrid.com/docs/for-developers/sending-email/rubyonrails/

    【讨论】:

    猜你喜欢
    • 2019-05-30
    • 2021-09-28
    • 2020-06-07
    • 2021-11-10
    • 2015-05-15
    • 1970-01-01
    • 2022-01-22
    • 2013-06-02
    • 2020-05-18
    相关资源
    最近更新 更多