Ruby Forum Ferret > acts_as_ferret + :tag_list, how to use it?

Posted by mixplate (Guest)
on 17.11.2006 17:43
hello,
i am currently using acts_as_ferret in one of my applications and now 
would like to also incorproate tags. i came across somesones blog that 
had this:

//////
Having Ferret index method results (like :tag_list) worked 
out-of-the-box for me with acts_as_ferret. I specified attributes (table 
columns) as strings and methods as symbols, like so:

acts_as_ferret :fields => [’title’, :tag_list]

I’ve had issues with corrupted indexes and various platform-related 
problems, but indexing and searching has been a breeze.
//////


now since tag_list symbol is part of my index, how do i access it or add 
to it?
where is this symbol stored? is it associated to my model that inheriets 
the acts_as_ferret plugin?

thanks.
Posted by Charlie Hubbard (chubbard)
on 18.11.2006 16:43
> now since tag_list symbol is part of my index, how do i access it or add 
> to it?
> where is this symbol stored? is it associated to my model that inheriets 
> the acts_as_ferret plugin?


I would use the :additional_fields option instead of fields so that you 
get all the default behavior plus the tag list.  For example:

acts_as_ferret( { :additional_fields => [:tag_list] } )

Then implement he tag_list methods to return tags concat'ed together. 
If you want limit your searches to just the tags you would do the 
following:

MyModel.find_by_contents( "tag_list:(#{my_tags})" )

Otherwise if you wanted to find any thing regardless of the fields just 
do a normal search without the field prepended.

Charlie
Posted by koloa (Guest)
on 18.11.2006 17:27
hello charlie, thank you for responding. sorry for my noob questions, 
but :tag_list will be a column in my model's database correct? so if i 
have a form with a text box for tags, i basically just save that text 
field in my column tag_list? or does the additional field option create 
a column in wherever ferret stores the indexes?

thanks.


i also would like to be able to fetch the most popular tags. would this 
be a method i define?
Posted by Charlie Hubbard (chubbard)
on 18.11.2006 19:39
koloa wrote:
> hello charlie, thank you for responding. sorry for my noob questions, 
> but :tag_list will be a column in my model's database correct? so if i 
> have a form with a text box for tags, i basically just save that text 
> field in my column tag_list? or does the additional field option create 
> a column in wherever ferret stores the indexes?

No, I assumed you'd be storing the tags in a seperate table like maybe 
your were using the acts_as_taggle plugin.  In this case AAF won't index 
model objects that are associations.  So in order to put them into your 
index you'd use the :additional_field option to include associations in 
your index.  When you use the :additional_field's option the value of 
that option is an array of symbols.  Those symbols will be turned into 
method calls on your object.  Then define a new method to return your 
data in a string so AAF can index it.  You could use an existing method 
just as well.

> i also would like to be able to fetch the most popular tags. would this 
> be a method i define?

I would not suggest using ferret for finding the most popular tags.  If 
you stored these in a seperate table it's an easy query to the database 
to figure that out.  Something like:

class MyTaggableObject < ActiveRecord::Base
  acts_as_ferret { :additional_fields => :tags }
  has_many :tags #( or acts_as_taggable, point is this is a many to 
many)

  def topTags( limit )
     topTags = MyTaggableObject.count( :all, :group => "tags.name", 
:include => :tags, :sort => "count", :limit => limit )
     # then build up your map or whatever.
  end
end

You'll have to check my math I'm just kinda hacking it out by memory, 
but its close.  I can remember how you specify the sorting when you're 
doing a count on a column.  (i.e. I can't remember what rails calls the 
column when doing a sort.  You'll need to figure that out in order to do 
the above).

While you could store the tags unnormalized in a column, I think you're 
gonna find it very hard to operate and build features on top of that. 
While you might be to use ferret to search.  Doing your next feature 
might not come so easy.  I would suggest you pull those out of the 
columns and put them in a proper table so you can use SQL to do your 
dirty work.

Charlie