Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to not include associated records? #67

Open
aguynamedben opened this issue Dec 17, 2020 · 3 comments
Open

Is there a way to not include associated records? #67

aguynamedben opened this issue Dec 17, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@aguynamedben
Copy link

My project users Rails, and has User and Organization models. An Organization can have many Users.

When I run ap Organization.first, I see the organization printed, but I also see nested data for organization.user. Is there a way to tell amazing_print to not recursively print records from associations?

.aprc supports

raw:           false,  # Do not recursively format instance variables.

but that appears to be something different. With it set to false I see recursive values from ActiveRecord relationships printed.

Thanks again for maintaining this library. 🙏

@HarlemSquirrel
Copy link
Member

I think this is something that is happening because of the values of attributes that happen to be other AR objects so we would likely need to add a configuration option for this and then check for nested AR objects and halt the recursion.
@paddor any thoughts?

@HarlemSquirrel HarlemSquirrel added the enhancement New feature or request label Jan 24, 2021
@paddor
Copy link
Contributor

paddor commented Jan 25, 2021

@aguynamedben I'm not actively using Rails or AR. What other recursive values are you seeing?

If you see other Organization objects, maybe an option that would disable detailed printing of any referenced objects of the same kind as the main object would make sense?

@gabrielcosta42
Copy link

@aguynamedben can you provide a minimal app where the issue you reported occurs?

I was able to reproduce the same output by adding another column named 'users' to Organization besides the association:

#<Organization:0x00007fe114059130> {
            :id => 1,
          :name => "Company",
         :users => [
        [0] #<User:0x00007fe11404dd58> {
                         :id => 1,
                       :name => "Robert",
            :organization_id => 1,
                 :created_at => Fri, 03 Dec 2021 12:07:37 UTC +00:00,
                 :updated_at => Fri, 03 Dec 2021 12:07:37 UTC +00:00
        }
    ],
    :created_at => Fri, 03 Dec 2021 12:07:16 UTC +00:00,
    :updated_at => Fri, 03 Dec 2021 12:07:16 UTC +00:00
}

Although it's a unlikely scenario it can be solved by ignoring the 'users' column or don't use ActiveRecord#send on awesome_active_record method.

class Organization < ApplicationRecord
   has_many :users

   self.ignored_columns = %w(users)
end
def awesome_active_record_instance(object)
   return object.inspect unless defined?(::ActiveSupport::OrderedHash)
   return awesome_object(object) if @options[:raw]

   data = if object.class.column_names == object.attributes.keys
            object.class.column_names.each_with_object(::ActiveSupport::OrderedHash.new) do |name, hash|
              if object.has_attribute?(name) || object.new_record?
                value = object.read_attribute(name) if object.respond_to?(name)
                hash[name.to_sym] = value
               end
            end
          else
            object.attributes
          end
   [awesome_simple(object.to_s, :active_record_instance), awesome_hash(data)].join(' ')
 end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants