У меня есть Post и Tag модель с ассоциацией "многие ко многим":
class Post < ActiveRecord::Base
attr_accessible :title, :content
has_many :taggings, :dependent => :destroy
has_many :tags, :through => :taggings
end
class Tag < ActiveRecord::Base
has_many :taggings, :dependent => :destroy
has_many :posts, :through => :taggings
has_many :subscriptions
has_many :subscribed_users, :source => :user, :through => :subscriptions
end
class Tagging < ActiveRecord::Base
belongs_to :post
belongs_to :tag
end
Я использую rails3-jquery-autocomplete:
posts_controller.rb:
автозаполнение: тег,: имя
routes.rb:
resources :posts do
get :autocomplete_tag_name, :on => :collection
end
_form.html.erb:
<div class="field">
<%= f.label :tag_names %><br />
<%= f.autocomplete_field :tag_names, autocomplete_tag_name_posts_path, :"data-delimiter" => ' ' %>
</div>
Я отлично работаю, но хотелось бы, чтобы в раскрывающемся списке автозаполнения отображалось количество сообщений с определенным тегом. Как это:
---------------------
| dr |
---------------------
|drinks (23) |
|drugs (4) |
| |
---------------------
(очень похоже на StackOverflow)
Буду признателен за любую информацию или пример кода.