How to Remove Associations in Ruby: Struggling with Circular Dependency Issue

This message was imported from the Ruby/Rails Modularity Slack server. Find more info in the import thread.

Message originally sent by slack user U721YFNO7M0

I’m struggling with how to remove associations.

My User has_many budgets and my Budget belongs_to
If I follow the advice I’ve seen in here about either (putting them in the same pack. Not applicable), or deleting one of the associations directions, I remove the has many. In it’s place, on budget I add something like

class Budget < ApplicationRecord
  def self.all_for_user(user_id)
    Budget.where(user_id: user_id)
  end
end

All that seems to do is replace instances of user.budgets with Budget.all_for_user(user.id) which still counts as a dependency from whoever is calling that to Budget.

Still trying to wrap my head around how that pattern solves the circular dependency issue.

All that seems to do is replace instances of user.budgets with Budget.all_for_user(user.id) which still counts as a dependency from whoever is calling that to Budget.

If you used this alternative everywhere, would UserPackage still need to depend on BudgetPackage?

Message originally sent by slack user U721YFNO7M0

The one place it’s depending on it is a service object for updating users and their budgets. I suppose that could be pulled into its own package (along with a few other “updater” style service objects). Nothing depends on the updater so that would break the circular.

It does feel like that updater should be part of the same package as user since they both deal with user, unless there is a way to think about it I’m missing.

I am going to send this message back to the channel, because <@U78EF4XFY8W> gave an answer to that. ^^