-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tag_names plucks multiple times #84
Comments
@Skulli I actually noticed something sort of related to your issue, but still digging in a bit. The biggest thing I found is mostly my own user error, which basically stemmed from abusing convenience methods like
For example, a case where it makes a lot more sense to just use the class PostsController < ApplicationController
def show
@post = Post.
.includes(:tags)
.friendly
.find(params[:id])
end
end <%# app/views/posts/show.html.erb %>
<%= render partial: 'posts/tag', collection: @post.tags, cached: true %>` <%# app/views/posts/_tag.html.erb %>
<%= link_to tag.name, tag_posts_path(tag_name: tag.name), class: 'badge bg-secondary text-decoration-none link-light', data: {turbo_action: 'replace', turbo_frame: "_top" } %> vs... things i was doing before, with zero consideration of eager loading or n+1 potential. Just in this simple example below i managed to basically break cache, yield a JOIN call to the layout and then again enumerate in the template as if they were just model properties of the instance. <%= content_for(:page_keywords, post.tags_as_string %>
<% cache post do %>
<% post.tag_names.each do |name| %>
<%= link_to name, tag_posts_path(tag_name: name), class: 'badge bg-secondary text-decoration-none link-light', data: {turbo_action: 'replace', turbo_frame: "_top" } %>
<% end %>
<% end %> So my first guess would be something really subtle is actually triggering those loads. I can't actually repo with |
Related to #84. It gets a bit complex in here - we want to ensure the dirty-state logic from ActiveRecord behaves properly - knowing both what tag names were and what they’ve become. I’ve switched the default reset value back to an empty array rather than nil, but we’re also resetting whether the value has been prepared, to ensure the latest is obtained from the database _when required_.
Thanks for reporting this @Skulli - it was indeed the fault of Gutentag, as part of the complexity of playing nicely with ActiveRecord's dirty state logic. If you want to give the latest commit a spin to confirm it works for you, that'd be appreciated - but either way, I'll try to get a new gem release out soon. Also, with regards to the auto-populating of tag names when an instance is loaded - I wasn't able to reproduce that, so, as you suggest, I think it might be something in your app. |
Just released v2.6.2 with this fix :) |
Have an existing Rails 7.0.3 app and installed that gem.
When calling a model with
tag_names
on anXYZ.last
Console puts out
Oddly when i do
Gutentag::ActiveRecord.call self
on the User-class (used with devise) and i do anUser.last
it calls tag_names directly. Seems the tag_names attribute is calculated on initialize. Did somebody had similar issues?
The second issue might be a different issue on my side, have to check wich addition causes that. Maybe paper_trail or something.
The text was updated successfully, but these errors were encountered: