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

Proposal to allow for overriding relationship getter #440

Open
lserman opened this issue Dec 1, 2022 · 1 comment
Open

Proposal to allow for overriding relationship getter #440

lserman opened this issue Dec 1, 2022 · 1 comment

Comments

@lserman
Copy link

lserman commented Dec 1, 2022

Hello, please correct me if I am wrong with my assumptions here.

My use-case is that I'd like to use has_one with a has_one_attached object. The issue is that the method on the model always returns this non-nil "proxy" object even when nothing is attached. For example:

class User
  has_one_attached :avatar
end

> User.new.avatar
#<ActiveStorage::Attached::One...

I think I've traced the logic to handle the relationships all the way to this proc:

if (records = @object.public_send(sideload_ref.association_name))
-- it simply uses public_send to call the association method which leads to issues down the line when a non-nil object is returned but is not backed by any data.

Unfortunately, I did not see a great way to resolve this without overriding the entire data_proc method with a monkey-patch that checks records.attached? and returns nil accordingly.

Is there a built-in way to do this I don't know about, and if not, what would be the preferred approach to allow for custom associations such as this?

@jasonkarns
Copy link
Contributor

@lserman I'm curious if you found a nifty solution or workaround?

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

No branches or pull requests

2 participants