Eager Load Depending on Type of Association in Ruby on Rails

后端 未结 7 2343
温柔的废话
温柔的废话 2021-02-07 22:13

I have a polymorphic association (belongs_to :resource, polymorphic: true) where resource can be a variety of different models. To simplify the questio

7条回答
  •  生来不讨喜
    2021-02-07 22:41

    If you instantiate the associated object as the object in question, e.g. call it the variable @object or some such. Then the render should handle the determination of the correct view via the object's class. This is a Rails convention, i.e. rails' magic.

    I personally hate it because it's so hard to debug the current scope of a bug without something like byebug or pry but I can attest that it does work, as we use it here at my employer to solve a similar problem.

    Instead of faster via preloading, I think the speed issue is better solved through this method and rails caching.

提交回复
热议问题