Thu Jun 14 11:31:00 UTC 2007

Using Scalable Acts As Taggable

Posted in Rails at 11:31 AM by matt

Did some major updates the scalable acts as taggable plugin for rails. This should make people Happy. Thanks for the feed back on the agile web development page. I appreciate it. go to your rails project
cd /path/to/rails/project/
<macro:jscode lang="ruby">
./script/plugin install svn://rubyforge.org/var/svn/scalabletagging/vendor/plugins/scalable_acts_as_taggable
The database needs two tables:
Taggings
`id` int(11) NOT NULL auto_increment,
`tag_id` int(11) default NULL,
`taggable_id` int(11) default NULL,
`taggable_type` varchar(255) default NULL,
`count` int(11) default '1',
#optional created_at dates

Tags
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`count` int(11) default '1',
#optional created_at dates
#-CONSOLE-
matt-carrs-computer:/usr/htdocs/zivitalia junior$ ./script/console
Loading development environment.
>>
-MYSQL-
matt-carrs-computer:/usr/htdocs/zivitalia junior$ mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 198 to server version: 5.0.27-standard

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> select * from tags;
Empty set (0.00 sec)

mysql> select * from taggings;
Empty set (0.00 sec)
-CONSOLE-
>> p = Photoset.new(:photographer_id => 20, :name => 'bha', :status => 'Uploaded', :photographer_license_id => 3)
=> #<Photoset:0x33edbec @new_record=true, @attributes={"status"=>"Uploaded", "name"=>"bha", "updated_at"=>nil, "photographer_license_id"=>3, "failed_processing_attempts"=>0, "processing"=>false, "created_at"=>nil, "photographer_id"=>20}>
>> p.save
=> true
>> c = Compliment.new(:user_id => 1, :photoset_id => 1, :public => 1, :text => 'hshshhlk lkj')
=> #<Compliment:0x33bf8dc @new_record=true, @attributes={"text"=>"hshshhlk lkj", "public"=>1, "user_id"=>1, "created_at"=>nil, "photoset_id"=>1}>
>> c.save
=> true

>> p.add_tag('cool, Awesome badass "This Rules"')
=> ["This Rules", "cool", "Awesome", "badass"]
-MYSQL-

mysql> select * from tags\G
*************************** 1. row ***************************
id: 17
name: this rules
count: 1
*************************** 2. row ***************************
id: 18
name: cool
count: 1
*************************** 3. row ***************************
id: 19
name: awesome
count: 1
*************************** 4. row ***************************
id: 20
name: badass
count: 1
4 rows in set (0.00 sec)

mysql> select * from taggings\G
*************************** 1. row ***************************
id: 21
tag_id: 17
taggable_id: 129
taggable_type: Photoset
count: 1
*************************** 2. row ***************************
id: 22
tag_id: 18
taggable_id: 129
taggable_type: Photoset
count: 1
*************************** 3. row ***************************
id: 23
tag_id: 19
taggable_id: 129
taggable_type: Photoset
count: 1
*************************** 4. row ***************************
id: 24
tag_id: 20
taggable_id: 129
taggable_type: Photoset
count: 1
4 rows in set (0.00 sec)
-CONSOLE-
>> p.add_tag('cool, awesome Badass "this rules"')
=> ["this rules", "cool", "awesome", "Badass"]
-MYSQL-

mysql> select * from tags\G
*************************** 1. row ***************************
id: 17
name: this rules
count: 2
*************************** 2. row ***************************
id: 18
name: cool
count: 2
*************************** 3. row ***************************
id: 19
name: awesome
count: 2
*************************** 4. row ***************************
id: 20
name: badass
count: 2
4 rows in set (0.00 sec)

mysql> select * from taggings\G
*************************** 1. row ***************************
id: 21
tag_id: 17
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 2. row ***************************
id: 22
tag_id: 18
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 3. row ***************************
id: 23
tag_id: 19
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 4. row ***************************
id: 24
tag_id: 20
taggable_id: 129
taggable_type: Photoset
count: 2
4 rows in set (0.00 sec)
-CONSOLE-
##NOTE - THIS IS c.add (COMPLIMENTS- not photosets)

>> c.add_tag('"This rules", badass, cool, AWESOME')
=> ["This rules", "badass", "cool", "AWESOME"]
-MYSQL-

mysql> select * from tags\G
*************************** 1. row ***************************
id: 17
name: this rules
count: 3
*************************** 2. row ***************************
id: 18
name: cool
count: 3
*************************** 3. row ***************************
id: 19
name: awesome
count: 3
*************************** 4. row ***************************
id: 20
name: badass
count: 3
4 rows in set (0.00 sec)

mysql> select * from taggings\G
*************************** 1. row ***************************
id: 21
tag_id: 17
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 2. row ***************************
id: 22
tag_id: 18
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 3. row ***************************
id: 23
tag_id: 19
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 4. row ***************************
id: 24
tag_id: 20
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 5. row ***************************
id: 25
tag_id: 17
taggable_id: 102
taggable_type: Compliment
count: 1
*************************** 6. row ***************************
id: 26
tag_id: 20
taggable_id: 102
taggable_type: Compliment
count: 1
*************************** 7. row ***************************
id: 27
tag_id: 18
taggable_id: 102
taggable_type: Compliment
count: 1
*************************** 8. row ***************************
id: 28
tag_id: 19
taggable_id: 102
taggable_type: Compliment
count: 1
8 rows in set (0.00 sec)
-CONSOLE-
>> Photoset.tag_counts
=> [#<Tag:0x3706798 @attributes={"name"=>"awesome", "id"=>"19", "count"=>"2"}>, #<Tag:0x3706748 @attributes={"name"=>"badass", "id"=>"20", "count"=>"2"}>, #<Tag:0x3706720 @attributes={"name"=>"cool", "id"=>"18", "count"=>"2"}>, #<Tag:0x37066f8 @attributes={"name"=>"this rules", "id"=>"17", "count"=>"2"}>]
>> Compliment.tag_counts
=> [#<Tag:0x3701a54 @attributes={"name"=>"awesome", "id"=>"19", "count"=>"1"}>, #<Tag:0x3701a2c @attributes={"name"=>"badass", "id"=>"20", "count"=>"1"}>, #<Tag:0x3701a04 @attributes={"name"=>"cool", "id"=>"18", "count"=>"1"}>, #<Tag:0x37019b4 @attributes={"name"=>"this rules", "id"=>"17", "count"=>"1"}>]
>>

>> Photoset.find_tagged_with("cool")
=> [#<Photoset:0x36a38dc @attributes={"status"=>"Uploaded", "name"=>"bha", "updated_at"=>"2007-06-13 23:14:33", "photographer_license_id"=>"3", "failed_processing_attempts"=>"0", "id"=>"129", "processing"=>"0", "created_at"=>"2007-06-13 23:14:33", "photographer_id"=>"20"}>]
>>
##NOTE-
tag_counts sends back an array of Tag objects.  So i can do
tags = Photoset.tag_counts
for tag in tags
  puts tag.count
  puts tag.name
  y Photoset.find_tagged_with(tag.name)  #return all photosets with tagged with that tag.
end
KEEP IN MIND Taggings count keeps track of the number of times that CLASS instantiation has been tagged a certain tag. Tags count keeps track of the number of times anything has been tagged that TAG.

Leave a Comment