pg_tags is a simple and high performance tagging gem for Rails using the PostgreSQL array type. An alternative to acts_as_taggable_on. This is still a work in progress.
Add this line to your application's Gemfile:
gem 'pg_tags'
And then execute:
bundle
Configure your Rails app to use Postgres and the pg adapter gem, if you haven't already. To start using pg_tags, you need to have an array column for your tags. See the example schema migration:
class CreatePost < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :tags, array: true, default: '{}'
t.timestamps
end
add_index :posts, :tags, using: 'gin'
end
end
GIN (Generalized Inverted Indexes) are best for static data such as tags because lookups are faster.
Next, run your migration:
rake db:migrate
Add the has_tags class annotation to your model. pg_tags uses the :tags column by default:
class Post < ActiveRecord::Base
has_tags
end
In your rails console: @post = Post.new(tags: ["classical", "jazz", "guitar"])
For models with multiple tag columns, you can do the following (remember to migrate and index each tag column):
```ruby
has_tags :tags, :authors
That's it! pg_tags gives you the following scope and class methods:
- with_any_#{tag_name}
- with_all_#{tag_name}
- without_any_#{tag_name}
- without_all_#{tag_name}
- all_#{tag_name}
- #{tag_name}_cloud
@post.tags = ["awesome", "slick"]
TODO
rake test
Alternatively, if you have rspec installed globally, call in the project root directory:
rspec
TODO
- Fork it ( http://github.com/Leventhan/pg_tags/fork )
- Create your feature branch (
git checkout -b feature/my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin feature/my-new-feature
) - Create new Pull Request