@saverio was successful on answer this database query question on Tagging from Scratch: the Tag Cloud Issue
Now I'm trying to connect the tagging system with the jQuery-tokenInput to Create and Find tags dynamically as on http://railscasts.com/episodes/258-token-fields-revised.
- My guess is that is a Query problem to the Posgresql Database.
- I've got Postgresql correctly installed
- jQuery-tokenInput is on its place on Application.js
//= require jquery.tokeninput
- Somehow it can load the tags from what was already on the database, but it fails querying the same words dynamically as listed below on pictures.js.coffee code.
Following all the relevant scope:
pictures.js.coffee
jQuery ->
$('#picture_tag_tokens').tokenInput '/tags.json'
theme: 'facebook'
prePopulate: $('#picture_tag_tokens').data('load')
/views/pictures/_form
<div class="field">
<%= f.label :tag_tokens, "Tags (separated by commas)" %><br />
<%= f.text_field :tag_tokens, data: {load: @picture.tags} %>
</div>
Here my logic get lost a bit
/models/picture.rb
class Picture < ActiveRecord::Base
attr_accessible :description, :title, :tag_tokens
has_many :taggings
has_many :tags, through: :taggings
attr_reader :tag_tokens
#The **below** is the relevant part for the #view/pictures/_form
def tag_tokens=(tokens)
self.tag_ids = Tag.ids_from_tokens(tokens)
end
def self.tagged_with(name)
Tag.find_by_name!(name).pictures
end
def self.tag_counts
Tag.select("tags.*, count(taggings.tag_id) as count").
joins(:taggings).group("tags.id")
end
def tag_list
tags.map(&:name).join(", ")
end
def tag_list=(names)
self.tags = names.split(",").map do |n|
Tag.where(name: n.strip).first_or_create!
end
end
end
Below I could figure out that I'm not being able to query the Database
/models/tag.rb
class Tag < ActiveRecord::Base
attr_accessible :name
has_many :taggings
has_many :pictures, through: :taggings
def self.tokens(query)
tags = where("name like ?", "%#{query}%")
if tags.empty?
[{id: "<<<#{query}>>>", name: "New: \"#{query}\""}]
else
tags
end
end
def self.ids_from_tokens(tokens)
tokens.gsub!(/<<<(.+?)>>>/) { create!(name: $1).id }
tokens.split(',')
end
end
And so was how I set my Tags controller behavior
#controllers/tags_controller.rb
class TagsController < ApplicationController
def index
@tags = Tag.all
respond_to do |format|
format.html
format.json { render json: @tags.tokens(params[:q]) }
end
end
end
So, Why I can't Query the Postgresql and I'm not able to Create or Find Dynamically?