ArgumentError:方法.group()必须包含参数

时间:2017-07-20 18:33:43

标签: ruby-on-rails activerecord

我正在使用Rails 5.0.2运行counter_culture gem,v1.6.2(升级到1.7当前不是我的选项)。我有ContactContactGroupGroup模型,如下所示:

class Contact < ApplicationRecord
    has_many :contact_groups
    has_many :groups, through: :contact_groups

    counter_culture [:contact_groups, :group], touch: true
    counter_culture [:contact_groups, :group],
        column_name: :confirmed_count,
        column_names: { ["contacts.confirmed_at IS NOT NULL"] => 'confirmed_count' },
        touch: true
end

class ContactGroup < ApplicationRecord
    belongs_to :contact
    belongs_to :group
    validates_uniqueness_of :contact_id, scope: :group_id
end

class Group < ApplicationRecord
    has_many :contact_groups
    has_many :contacts, through: :contact_groups
end

根据我从阅读文档中的理解,这个设置是正确的。如果我运行Contact.counter_culture_fix_counts,一切正常。如果我尝试创建新联系人,我会收到以下错误。

c = Contact.new(attrs)
[9] pry(main)> #<Contact id: nil, first_name: "Billy", last_name: "The Kid", email: "bkid@example.com" ...>
c.valid?
[10] pry(main)> true
c.save
[11] pry(main)> c.save
(0.2ms)  BEGIN
Contact Exists (0.5ms)  SELECT  1 AS one FROM "contacts" WHERE "contacts"."email" = 'bkid@example.com' LIMIT 1
Contact Exists (0.5ms)  SELECT  1 AS one FROM "contacts" WHERE "contacts"."email" = 'bkid@example.com' AND "contacts"."company_id" = 94 LIMIT 1
Contact Exists (0.6ms)  SELECT  1 AS one FROM "contacts" WHERE "contacts"."preferences_token" = '6dc8b7943f87efd81477450c199e6ecf' LIMIT 1
SQL (1.2ms)  INSERT INTO "contacts" ("first_name", "last_name", "email", "created_at", "updated_at", "company_id", "color", "encrypted_password", "confirmation_token", "confirmation_sent_at", "unconfirmed_email", "request_email_frequency", "invite_email_sent_at", "preferences_token", "key") VALUES ('Billy', 'The Kid', 'bkid@example.com', '2017-07-20 18:24:16.880278', '2017-07-20 18:24:16.880278', 94, '#344B59', '$2a$10$baAhoJtROfmJRx1ymb7aX.5s/5u.99..fT9OsC9MvoKEpmT4olq.G', '6dc8b7943f87efd81477450c199e6ecf', '2017-07-20 17:50:50.005270', 'bkid@example.com', 'Daily', '2017-07-20 17:50:50.033649', '6dc8b7943f87efd81477450c199e6ecf', 'Billy-dedf62') RETURNING "id"
(0.3ms)  ROLLBACK
ArgumentError: The method .group() must contain arguments.
from /Users/ACIDSTEALTH/.gem/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/relation/query_methods.rb:1214:in `check_if_method_has_arguments!'

我一直在努力弄清楚造成这种情况的原因已经有一段时间了,到目前为止,我唯一的工作理论是与我的模型Group和ActiveRecord&#39; s .group()方法。我考虑将Group重新命名为其他内容,但这显然有很多工作要做,所以我想首先找出我的理论是否正确,如果可能的话。想法?

1 个答案:

答案 0 :(得分:0)

问题是您在定义关系

时使用了保留字group

belongs_to :group与ActiveRecord的group方法http://guides.rubyonrails.org/active_record_querying.html#group产生冲突。

唯一的解决方案是为关系使用不同的名称