marcamillion
marcamillion

Reputation: 33765

How do I explore the results of an ActiveRecord relation?

This is what I am doing:

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

But not sure how to explore that to see what records apply to that relation.

I have a Connection model, that has the following associations:

# == 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

And I have a Membership model that looks like this:

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

Upvotes: 3

Views: 2191

Answers (1)

svelandiag
svelandiag

Reputation: 4320

#<Connection::ActiveRecord_Relation:0x3fe19aa2ce28> means your query has something wrong, more info here

Regarding explore the results of your query: Well there are 2 ways in which you can take a look, the easier is to add .all at the end of your query, and the other is to do the following in your prompt

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

Using first, second, third, last will show you the the query you are applying then you can take a look and debug what's wrong with your query

Upvotes: 1

Related Questions