Rails NoMethodError(#<ActiveRecord::Relation []> 的未定义方法“<<”):

Rails NoMethodError (undefined method `<<' for #<ActiveRecord::Relation []>):

背景故事

我目前正在将 Rails 应用上的 Ruby 更改为多数据库配置。切换的主要原因是将我的成员(用户)和配置文件表放在一个单独的数据库中,可以从另一个 RoR 应用程序访问它;因此,除了使用 OAuth 和 Doorkeeper 之外,我还拥有单点登录功能。这是一个很多小时的项目,有很多疯狂的障碍。今晚终于似乎一切正常,直到我 运行 我的规格测试,我今天所做的一项工作是抛出标志。

这是问题的适当代码,为简洁起见进行了简化。

如有任何帮助,我们将不胜感激。谢谢。

型号

app/models/members_record.rb

class MembersRecord < ApplicationRecord
    self.abstract_class = true
  
    connects_to database: { writing: :members, reading: :members }
end

app/models/member.rb

class Member < MembersRecord
    devise  :database_authenticatable, :registerable,
            :recoverable, :rememberable, :trackable, 
            :validatable, :confirmable
  
    has_one :member_map,    dependent: :destroy
    has_one :profile,       dependent: :destroy
    has_one :site_profile,  dependent: :destroy
    
    has_many :approved_group_members, -> { approved }, class_name: "GroupMember"
    has_many :groups, through: :approved_group_members, class_name: "Group", source: :group

app/models/group.rb

class Group < ApplicationRecord
    belongs_to :owner, class_name: 'Member'
    belongs_to :organization, optional: true
  
    has_many :group_members, dependent: :destroy
  
    # Many of the lines below no longer work with Multiple DBs, as we can not INNER JOIN across DBs.
    # They have been commented out and methods built in their place.
  
    has_many :approved_group_members, -> { approved }, class_name: "GroupMember"
    # has_many :members, through: :approved_group_members, class_name: "Member", source: :member
    def members
      Member.where(id: self.approved_group_members.pluck(:member_id))
    end
  
    has_many :invited_group_members, -> { invited }, class_name: "GroupMember"
    # has_many :invited_members, through: :invited_group_members, class_name: "Member", source: :member
    def invited_members
      Member.where(id: self.invited_group_members.pluck(:member_id))
    end

app/models/group_member.rb

class GroupMember < ApplicationRecord
  belongs_to :group
  belongs_to :member

  enum status: { approved: 0, invited: 1, pending_approval: 2, banned: 3}
end

解决方法

现在,查看上面的代码,您应该会在组模型中看到一些有问题的解决方法。原始代码已被注释掉,下面是解决方法。这只是代码:

# Many of the lines below no longer work with Multiple DBs, as we can not INNER JOIN across DBs.
# They have been commented out and methods built in their place.

has_many :approved_group_members, -> { approved }, class_name: "GroupMember"
# has_many :members, through: :approved_group_members, class_name: "Member", source: :member
def members
  Member.where(id: self.approved_group_members.pluck(:member_id))
end

has_many :invited_group_members, -> { invited }, class_name: "GroupMember"
# has_many :invited_members, through: :invited_group_members, class_name: "Member", source: :member
def invited_members
  Member.where(id: self.invited_group_members.pluck(:member_id))
end

这些解决方法的原因是因为 has_many 关系无法与 class Member 一起使用,一旦 class 引用了不同数据库中的数据。正在构建的 SQL 查询失败。因此,我改为创建了一个方法,并在 Member 模型上调用了 where。这给了我正在寻找的成员名单。我必须在 9 次不同的情况下执行此操作。

现在我遇到的问题

在我的 100 多个不同位置的代码中,我将一个成员或其他对象添加到给定的列表中,例如使用此类代码的 'invited members'。 (这段代码特别是在我的规范之一中,现在失败了)

it 'will set the GroupMember to approved if the group is invite only and member was invited' do
  group_invitation
  group.invited_members << member
  expect { service.call! }.to change{GroupMember.count}.by(0)
  expect(service.success?).to be_truthy
  expect(service.success_message).to eq("You have successfully joined this group.")
  expect(service.error).to eq("")
end

第二行显示 group.invited_members << member 然后我收到一条错误消息:

NoMethodError (undefined method `<<' for #<ActiveRecord::Relation []>):

我的问题

我完全理解错误的意思,但不知道如何更正它。有没有其他方法可以让 << 保留下来?我非常希望只需要更改 9 次不同的时间,而不是重写 100 多个不同的实例,其中 << 用于将成员或任何对象添加到被引用的列表中.

如果您需要更多详细信息,请告诉我。再次感谢!

Rails 7 及更高版本原生支持此功能。 (参见 https://edgeguides.rubyonrails.org/active_record_multiple_databases.html#handling-associations-with-joins-across-databases, https://github.blog/2021-07-12-adding-support-cross-cluster-associations-rails-7/ and https://github.com/rails/rails/pull/41937

如果你真的需要这个,我可能会建议转向 rails master,并使用原生方式:

has_many :members, 
         through: :approved_group_members, 
         class_name: "Member", 
         source: :member, 
         disable_joins: true
  1. 我想和rewritten一起去,你可以和rails的主人一起去。

  2. 您可以尝试将对象从activerecord关系转换为 活动记录收集代理对象,我们 << 这个 method.Which 我认为这不是一个好方法