pat / gutentag

A good, simple, solid tagging extension for ActiveRecord.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Does it make sense to define a :with_tag scope by default?

etdsoft opened this issue · comments

I'm finding that I need to add this to almost every Taggable object:

  scope :with_tag, ->(tag_name){ joins(:tags).where('gutentag_tags.name' => tag_name)

Maybe it would be a nice convenience scope that the library could include by default. It'd also isolate the user from having to know the internal table name.

My 2c, feel free to dismiss.

If you find you're doing this in every model, then I'd recommend pulling it out into a module. Perhaps something like this would work:

module Taggable
  def has_many_tags
    super
    scope :with_tag, ->(tag_name) {
      joins(:tags).where('gutentag_tags.name' => tag_name)
    }
  end
end

And then in each model:

extend Taggable
has_many_tags

sure thing, feel free to close. With this solution however, the app code is still bound to the internal table name. If you decide to change the name inside the gem for whatever reason you break the apps depending on it. Not a big thing though.

Thanks for the lib!