eager loading - How to correctly use includes to solve N+1 rails -
i have 3 models:
1)contenttype (parent)
2)recenttools (last 3 childs of contenttype, model tool, recent_tools association using scope of tool)
3)tooltranslation (child of tool)
i want solve n+1, , write includes this. right im stucked:
contenttype.includes(recent_tools: :tool_translation)
this should work guess accorting guides, includes always returns me full list of tools, instead of recent_tools, , of translations them.
this tool.rb
class tool < applicationrecord has_one :tool_translation, -> { where(locale: i18n.locale) }, dependent: :destroy belongs_to :content_type scope :recent, ->(number){ limit(number) } end
and content_type model:
class contenttype < applicationrecord has_many :tools, dependent: :destroy has_many :recent_tools, -> { recent(3) }, class_name: "tool" end
where missing? can explain? maybe bad logic recent tools(it needed because need 3 last tools of content_type on main page)? how should write includes correctly? thanks!
hope work
contenttype.includes(:recent_tools, recent_tools: :tool_translation)
in case not, please let me know.
Comments
Post a Comment