Alternative method for proxy_owner in ActiveRecord

后端 未结 1 1287
耶瑟儿~
耶瑟儿~ 2021-01-06 06:27

ActiveRecord proxy_owner is now deprecated and the explanation here is very vague on how to change it, so I\'m not sure how to use it my case: http://apidock.com/rails/Acti

相关标签:
1条回答
  • 2021-01-06 06:59

    I think it's safer to send :owner to proxy_association instead:

    class Library < ActiveRecord::Base
      has_many :books do
        def some_method
          proxy_association.owner.author
        end
      end
    end
    

    The use of proxy_association is now mentioned in the documentation:

    However, inside the actual extension code, you will not have access to the record (record.association(:items).owner) as above. In this case, you can access proxy_association. For example, record.association(:items) and record.items.proxy_association will return the same object, allowing you to make calls like proxy_association.owner inside association extensions.

    0 讨论(0)
提交回复
热议问题