Skip to content

Proposal to allow for overriding relationship getter #440

Open
@lserman

Description

@lserman

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?

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