gpt4 book ai didi

ruby-on-rails - 我如何探索 ActiveRecord 关系的结果?

转载 作者:行者123 更新时间:2023-12-04 18:01:52 25 4
gpt4 key购买 nike

这就是我正在做的:

[72] pry(main)> Connection.joins(:inviter_memberships).where(inviter_memberships: { invited: c.inviter_membership.invited })
=> #<Connection::ActiveRecord_Relation:0x3fe19aa2ce28>

但不确定如何探索它以查看哪些记录适用于该关系。

我有一个 Connection 模型,它具有以下关联:

# == Schema Information
#
# Table name: connections
#
# id :integer not null, primary key
# membership_id :integer
# invited_membership_id :integer
# truncated for brevity

class Connection < ActiveRecord::Base
belongs_to :inviter_membership, class_name: "Membership", foreign_key: "membership_id"
belongs_to :invited_membership, class_name: "Membership", foreign_key: "invited_membership_id"
end

我有一个如下所示的Membership 模型:

class Membership < ActiveRecord::Base
belongs_to :inviter, class_name: "User", foreign_key: "user_id"
belongs_to :invited, class_name: "User", foreign_key: "invited_id"
has_many :connections, dependent: :destroy
end

最佳答案

#<Connection::ActiveRecord_Relation:0x3fe19aa2ce28>表示您的查询有问题,更多信息 here

关于探索您的查询结果:好吧,您可以通过两种方式查看,最简单的是添加 .all在您的查询结束时,另一个是在您的提示中执行以下操作

Connection.joins(:inviter_memberships).where(inviter_memberships: { invited: c.inviter_membership.invited }).each do |record|
puts "---------"
puts record
end

使用 first , second , third , last将向您显示您正在应用的查询然后您可以查看并调试您的查询有什么问题

关于ruby-on-rails - 我如何探索 ActiveRecord 关系的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33931223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com