administrate icon indicating copy to clipboard operation
administrate copied to clipboard

How could I make a delegated object behave like a belongs_to?

Open 8vius opened this issue 7 years ago • 3 comments

I'm delegating my user on one of my dashboard models:

delegate :user, to: :property

The model belongs to the property so I want to be able to display that delegated user as a regular Field::BelongsTo is there a way to achieve this?

8vius avatar Oct 22 '18 17:10 8vius

Have you found a solution?

danturu avatar Jun 06 '19 20:06 danturu

Would it be possible to use has_one+through instead of delegate? Something like this:

class MyModel < ApplicationRecord
  belongs_to :property
  has_one :user, through: :property
end

I tried this in the example app and it worked. I could add user as a BelongsTo field to my dashboard.

pablobm avatar Apr 22 '21 15:04 pablobm

Using the has_one through just worked for me in this scenario. Although I'm a little concerned about changes to the performance of other unrelated code in the codebase that was using the delegate method and is now using the has_one through method.

dedman avatar Jan 26 '23 19:01 dedman