Skip to content

inverse_of returns nil when enriching has_many association with updates #258

@gazayas

Description

@gazayas

inverse_of returns nil

I'm running into a strange issue where inverse_of returns nil although I have declared it in the model.

For example, I have two models, Listing which has many Listings::PublishActions.

# Listing
has_many :publish_actions, class_name: "Listings::PublishAction", dependent: :destroy, foreign_key: :listing_id, enable_updates: true, inverse_of: :listing

# Listing::PublishAction
belongs_to :listing, class_name: "Listing"

Inside of enrich_association_with_updates, this line gets the inverse_of reflection:

inverse_of = reflection.inverse_of&.name&.to_s

This is returning nil for me, and I get an error from the collection registry.

inverse_name has the proper value

Although this is nil, inverse_name has the proper key, and the options have the inverse_of declaration inside of it as well:

[5] pry(Listing)> self
=> Listing(id: integer, team_id: integer, name: string, created_at: datetime, updated_at: datetime)
[6] pry(Listing)> name
=> :publish_actions
[7] pry(Listing)> reflect_on_association(name)
=> #<ActiveRecord::Reflection::HasManyReflection:0x00007f4b1e4d97f0
 @active_record=
  Listing(id: integer, team_id: integer, name: string, created_at: datetime, updated_at: datetime),
 @class_name="Listings::PublishAction",
 @inverse_name=:listing,
 @inverse_of=nil,
 @klass=
  Listings::PublishAction(id: integer, listing_id: integer, started_at: datetime, completed_at: datetime, target_count: integer, performed_count: integer, scheduled_for: datetime, sidekiq_jid: string, created_by_id: integer, approved_by_id: integer, created_at: datetime, updated_at: datetime),
 @name=:publish_actions,
 @options=
  {:class_name=>"Listings::PublishAction",
   :dependent=>:destroy,
   :foreign_key=>:listing_id,
   :inverse_of=>:listing,
   :extend=>[ExtendHasMany::ClassMethods, ObfuscatesId::ClassMethods]},
 @plural_name="publish_actions",
 @scope=nil>
[8] pry(Listing)> reflect_on_association(name).inverse_of
=> nil

This doesn't happen however when firing up the Rails console and grabbing the reflection that way:

> rails c
Loading development environment (Rails 7.0.4.2)
irb(main):001:0> Listing.reflect_on_association(:publish_actions).inverse_of
=> 
#<ActiveRecord::Reflection::BelongsToReflection:0x00007f50ddbdb098                                
 @active_record=Listings::PublishAction (call 'Listings::PublishAction.connection' to establish a connection),
 @klass=nil,                                                                                      
 @name=:listing,                                                                                  
 @options={:class_name=>"Listing"},                                                               
 @plural_name="listings",                                                                         
 @scope=nil>   

Also, everything works by simply removing enable_updates: true, which led me to believe it's something going on here. For what it's worth, it's happening in a Bullet Train application, but it doesn't seem like it's something there. I wish I could give more details, but that's about all I can glean from the code right now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions