使用注释器gem时,解决具有现有订阅模型的应用程序

时间:2019-04-22 07:36:11

标签: ruby-on-rails-4

我正在使用commontator gem来实现评论功能。但是存在一个问题,因为该gem也使用订阅模型,而我们的gem也使用订阅模型。 因此,有一个与commontator的订阅表关联的thread_id列。但是由于它正在访问我们的订阅模型,...它给出了一个错误PG::UndefinedColumn: ERROR: column subscriptions.thread_id does not exist

对于实际上给出错误且具有关联has_many: subscriptions的线程模型...我按如下方式添加了类名

module Commontator
class Thread < ActiveRecord::Base
belongs_to :closer, polymorphic: true, optional: true
belongs_to :commontable, polymorphic: true, optional: true
has_many :comments, dependent: :destroy
has_many :subscriptions, class_name: "Commontator::Subscription", dependent: :destroy

但这仍然会返回我的订阅模型:

pry(#<Commontator::Thread>)> subscriber.subscriptions
=> [#<Subscription:0x00007fabcc5d3340
  id: 1,
  kind: "free",
  end_date: nil,
  user_id: 2,
  channel_id: 1,
  created_at: Thu, 04 Apr 2019 11:21:15 UTC +00:00,
  updated_at: Thu, 04 Apr 2019 11:21:15 UTC +00:00>]

我的user.rb包含has_many :subscriptions, dependent: :destroy

Commontator的订阅模型与my_subscription模型

irb(main):017:0> Commontator::Subscription.new
=> #<Commontator::Subscription id: nil, subscriber_type: nil, subscriber_id: nil, thread_id: nil, created_at: nil, updated_at: nil>
irb(main):018:0> Subscription.new
=> #<Subscription id: nil, kind: "free", end_date: nil, user_id: nil, channel_id: nil, created_at: nil, updated_at: nil>

是否可以通过某种方式在不更改订阅模型名称的情况下进行处理?

1 个答案:

答案 0 :(得分:0)

@vaishnavi,commontator Rails引擎要求您将acts_as_commontator添加到模型中,然后才能对其进行评论。

在大多数Rails应用程序中,显然其中包括User模型

这是PR,其中需要进行更改才能解决此问题。

请尝试在您的gem 'commontator', git: "git@github.com:pikender/commontator.git", branch: "fixes/issue-135"中使用Gemfile,并通过测试和确认是否为您解决了问题来帮助其他人。

希望这可以解决您的问题并编写出满意的代码:)


在此处添加更改差异以供将来参考,以防上述PR被删除或丢失

需要更改很容易将subscriptions关联更改为commontator_subscriptions并更改其引用仅两个(感觉很幸运:))

diff --git a/app/models/commontator/thread.rb b/app/models/commontator/thread.rb
index 83e51e3..b410b23 100644
--- a/app/models/commontator/thread.rb
+++ b/app/models/commontator/thread.rb
@@ -91,7 +91,7 @@ module Commontator

     def subscription_for(subscriber)
       return nil if !subscriber || !subscriber.is_commontator
-      subscriber.subscriptions.where(thread_id: self.id).first
+      subscriber.commontator_subscriptions.where(thread_id: self.id).first
     end

     def subscribe(subscriber)
diff --git a/lib/commontator/acts_as_commontator.rb b/lib/commontator/acts_as_commontator.rb
index 1497ebb..2fe11a6 100644
--- a/lib/commontator/acts_as_commontator.rb
+++ b/lib/commontator/acts_as_commontator.rb
@@ -17,12 +17,12 @@ module Commontator

           has_many :comments,      as: :creator,
                                    class_name: 'Commontator::Comment'
-          has_many :subscriptions, as: :subscriber,
+          has_many :commontator_subscriptions, as: :subscriber,
                                    class_name: 'Commontator::Subscription',
                                    dependent: :destroy
         end
       end
diff --git a/spec/lib/commontator/acts_as_commontator_spec.rb b/spec/lib/commontator/acts_as_commontator_spec.rb
index 11a9cda..cade863 100644
--- a/spec/lib/commontator/acts_as_commontator_spec.rb
+++ b/spec/lib/commontator/acts_as_commontator_spec.rb
@@ -17,7 +17,7 @@ module Commontator
     it 'must modify models that act_as_commontator' do
       user = DummyUser.create
       expect(user).to respond_to(:comments)
-      expect(user).to respond_to(:subscriptions)
+      expect(user).to respond_to(:commontator_subscriptions)
       expect(user).to respond_to(:commontator_config)
       expect(user.commontator_config).to be_a(CommontatorConfig)
     end