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

Faster way to access prefix IDs for associated models #84

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

cjavdev
Copy link
Contributor

@cjavdev cjavdev commented Nov 13, 2024

We started to bump up against performance issues when rendering lots of prefix IDs for subordinate objects in API responses. For example:

class Project < ARBase
  belongs_to :account
end

This causes an N+1 to fetch the account just to render it's prefix ID.

json.id project.prefix_id
json.account_prefix_id project.account.prefix_id

This also causes issues when we go to create a project with a prefix ID. Consider this:

project = Project.create!(account_id: "acct_abc123")

This fails unless we override the account_id setter method on Project so that it first finds the Account and then uses the found account's underlying ID like:

class Project
  belongs_to :account

  def account_id=(raw)
    super Account.find(raw).id # Has other deeper problems if not found
  end
end

@excid3
Copy link
Owner

excid3 commented Nov 21, 2024

Thanks @cjavdev! This is a great improvement.

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

Successfully merging this pull request may close these issues.

2 participants