# Acts\_as\_taggable and foreign keys

**URL:** <https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000>\
**Category:** Rails\
**Created:** [July 14, 2008, 11:33am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000 "2008-07-14T11:33:01Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 14, 2008, 11:33am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/1 "2008-07-14T11:33:01Z")

</div>

Hi,

I’ve developed an application last year with rails 1.2.3 and it worked  
fine on the web server till recently. Not sure what started the  
problem but I am having trouble with acts\_as\_taggable gem (which I  
know has been depreciated).

It was suggested to look into has\_many\_polymorphs but I ran into  
problems with updating my database. The database has 3 tables  
(:products, :tags, :products\_tags) and uses foreign keys.  
The application is based on “Beginning RoR e\_commerce”. Tags table has  
2 columns: id and name.

So, I gave that up on that one and tried to find an easier solution.  
Other things I looked at:

- I installed acts\_as\_taggable as plugin instead of a gem – but I’m  
just not sure what to change in the application in order to get it to  
work
- I installed acts\_as\_taggable\_on\_steroids – but again, I think I ran  
into the same problem with the foreign keys. This is the error message  
I get when I try “rake db:migrate”

```auto
Mysql::Error: Table 'tags' already exists: CREATE TABLE tags (`id`
int(11) DEFAULT NULL auto_increment PRIMARY KEY, `name` varchar(255)
DEFAULT NULL) ENGINE=InnoDB
```

Would anyone have a solution on how to fix my problem?  
Thanks in advance,  
Elle

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 14, 2008, 11:38am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/2 "2008-07-14T11:38:19Z")

</div>

On 14 Jul 2008, at 10:32, elle wrote:

> ```auto
> Mysql::Error: Table 'tags' already exists: CREATE TABLE tags (`id`
> int(11) DEFAULT NULL auto_increment PRIMARY KEY, `name` varchar(255)
> DEFAULT NULL) ENGINE=InnoDB
> ```

I assume acts\_as\_taggable\_on\_steroids is trying to create a tags table  
that you already have. I’d just blow away that migration (after  
checking that the table it was trying to create matches the one you  
already have.

Fred

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 15, 2008, 8:42am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/3 "2008-07-15T08:42:24Z")

</div>

Thanks Fred. I did just that. I also dropped my products\_tags table  
because as I understand the ‘taggings’ table created by the plugin  
acts as the connector. Now, I don’t care about losing that data in  
this case, I can always enter it manually (and I am happy not to be  
using a depreciated gem)… but I feel hopeless as I am not sure how  
to make it work from now on.

What I’ve done so far is:

1. Installed plugin
2. Added db table: taggings. Already had tags with id and name column.
3. Added acts\_as\_taggable to my Product class

When I login as admin to try and tag the products manually, I get this  
error message (when I have both acts\_as\_taggable and  
acts\_as\_taggable\_on\_steroids plugins installed):

NoMethodError in ProductController#update  
undefined method `tag\_collection\_name’ for Product:Class

My update method includes the following:

def update  
@product = Product.find(params[:id])  
@product.tag(params[:tags], :separator =\> ‘,’, :clear =\> true)

```
if @product.update_attributes(params[:product])
  flash[:notice] = 'Product was successfully updated.'
  redirect_to :action => 'show', :id => @product
else
  load_data
  render :action => 'edit'
end

```

end

private

```
def load_data
  @categories = Category.find(:all)
  @tags = Tag.find(:all)
end

```

I am just not sure how to make the connection between the tag and the  
product.  
Help. Please.

Elle

On Jul 14, 9:37 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 15, 2008, 9:58am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/4 "2008-07-15T09:58:42Z")

</div>

On Jul 15, 7:41 am, elle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. Added acts\_as\_taggable to my Product class
> 
> When I login as admin to try and tag the products manually, I get this  
> error message (when I have both acts\_as\_taggable and  
> acts\_as\_taggable\_on\_steroids plugins installed):

First off, you’re probably asking for trouble by having both installed

Secondly, have you tried just looking at the examples in the  
acts\_as\_taggable\_on\_steroids documentation ?

Fred

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 15, 2008, 10:21am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/5 "2008-07-15T10:21:31Z")

</div>

I don’t have tag\_list method. I haven’t used it with the gem. My tag  
controller has:

def list  
@page\_title = ‘Listing tags’  
@tags = Tag.find(:all)  
@tag\_pages, @tags = paginate :tags, :order =\> :name, :per\_page =\>  
10  
end  
def show  
# tag = params[:id]  
@tag = Tag.find(params[:id])  
@page\_title = “Products tagged with ‘#{tag}’”  
@products = Product.find\_tagged\_with(:any =\> tag, :separator =\>  
‘,’)  
end

and Tag class has:

has\_and\_belongs\_to\_many :products

Any suggestions to how I could fix this?

Thanks,  
Elle

On Jul 15, 8:11 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 15, 2008, 10:05am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/6 "2008-07-15T10:05:43Z")

</div>

> First off, you’re probably asking for trouble by having both installed

Well, when I deleted acts\_as\_taggable, I got an error of:

Processing ProductController#update (for 127.0.0.1 at 2008-07-15  
18:35:50) [POST]  
Parameters: {“commit”=\>“Edit”, “tags”=\>“LS750”, “product”=\>{…},  
“action”=\>“update”, “id”=\>“1”, “controller”=\>“product”}  
[…]  
NoMethodError (undefined method `tag’ for #Product:0x26064fc):

> Secondly, have you tried just looking at the examples in the  
> acts\_as\_taggable\_on\_steroids documentation ?

Yes I have but I guess I’m just too new to easily know how to  
implement it all 🙂

Advice?

Thanks,  
Elle

On Jul 15, 7:58 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 15, 2008, 10:11am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/7 "2008-07-15T10:11:35Z")

</div>

On Jul 15, 9:05 am, elle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Secondly, have you tried just looking at the examples in the  
> > acts\_as\_taggable\_on\_steroids documentation ?
> 
> Yes I have but I guess I’m just too new to easily know how to  
> implement it all 🙂

Well accordeing to those docs tag doesn’t exists: all those examples  
show manipulation of tag\_list (tag\_list.add, tag\_list.remove etc…)

Fred

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 15, 2008, 11:14am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/8 "2008-07-15T11:14:17Z")

</div>

On Jul 15, 9:20 am, elle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t have tag\_list method. I haven’t used it with the gem. My tag  
> controller has:

If you look at acts\_as\_taggable\_with\_steroids, it adds that method to  
your models (when you call acts\_as\_taggable).  
I think you’re getting confused because you think  
acts\_as\_taggable\_with\_steroids is a drop in replacement for  
acts\_as\_taggable which it would appear it isn’t.  
The examples in its documentation look pretty straightforward, I  
suggest you forget what you know about acts\_as\_taggable and look at  
those instead.

Fred

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 16, 2008, 9:33am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/9 "2008-07-16T09:33:28Z")

</div>

One more question. When I am still in console, I added a tag to  
p.tag\_list, then when I want to save the product (p.save) I get the  
following error:

NoMethodError: undefined method `find_or_create_with_like_by_name' for Tag:Class from ./script/../config/../config/../vendor/rails/activerecord/ lib/active_record/base.rb:1235:in`method\_missing’  
from ./script/…/config/…/config/…/vendor/plugins/  
acts\_as\_taggable\_on\_steroids/lib/acts\_as\_taggable.rb:182:in  
`save_tags' from ./script/../config/../config/../vendor/plugins/ acts_as_taggable_on_steroids/lib/acts_as_taggable.rb:181:in`each’  
from ./script/…/config/…/config/…/vendor/plugins/  
acts\_as\_taggable\_on\_steroids/lib/acts\_as\_taggable.rb:181:in  
`save\_tags’  
…

Would anyone know why I get this error?

Thanks,  
Elle

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 16, 2008, 8:47am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/10 "2008-07-16T08:47:12Z")

</div>

I agree the examples do look straightforward. But as usual, I still  
have questions:  
I started the console and did the following:

> > p = Product.find(:first)  
> > =\>#\<Product:0x2285ad0 @attributes=…  
> > p.tag\_list.add(“tagnamehere”)  
> > =\> [“tagnamehere”]  
> > p  
> > =\>#\<Product:0x2285ad0 @tag\_list=[“tagnamehere”], @tags=, …

Now, I don’t understand what is the difference between @tag\_list and  
@tags.

I’ve also looked at [http://taggable.rubyforge.org/](http://taggable.rubyforge.org/) and tried to use:  
p.tag → error: NoMethodError: undefined method `tag_model' for Product:Class p.tag_with --> error: NoMethodError: undefined method `tag\_with’ for  
#Product:0x2285ad0  
p.tag.add → ArgumentError: wrong number of arguments (0 for 1)  
from (irb):7:in `tag’  
from (irb):7

Elle

On Jul 15, 9:13 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 16, 2008, 9:44am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/11 "2008-07-16T09:44:01Z")

</div>

On 16 Jul 2008, at 07:46, elle wrote:

> Now, I don’t understand what is the difference between @tag\_list and  
> @tags.

internal implemenation details

> I’ve also looked at [http://taggable.rubyforge.org/](http://taggable.rubyforge.org/) and tried to use:  
> p.tag → error: NoMethodError: undefined method `tag_model' for Product:Class p.tag_with --> error: NoMethodError: undefined method `tag\_with’ for  
> #Product:0x2285ad0  
> p.tag.add → ArgumentError: wrong number of arguments (0 for 1)  
> from (irb):7:in `tag’  
> from (irb):7

I think you will definitely confuse things by chopping and changing  
between multiple tagging libraries all with slightly different  
semantics (and which could easily conflict when used simultaneously)

Fred

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 16, 2008, 9:44am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/12 "2008-07-16T09:44:36Z")

</div>

On 16 Jul 2008, at 08:32, elle wrote:

> One more question. When I am still in console, I added a tag to  
> p.tag\_list, then when I want to save the product (p.save) I get the  
> following error:

That implies that you’ve got your own tag model in app/models which is  
taking precedence over the one bundled in the plugin.

Fred

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 16, 2008, 12:35pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/13 "2008-07-16T12:35:05Z")

</div>

On 16 Jul 2008, at 09:57, elle wrote:

> Q1. I’ve been having problems trying to update a product. My  
> product\_controller.rb#update has:  
> @product.tag(params[:tags], :separator =\> ‘,’, :clear =\> true)
> 
> When I try to update a product I get this error:  
> NoMethodError in ProductController#update  
> undefined method `tag’ for #Product:0x23e6dd4

according to the plugin docs you should be using tag\_list (ie  
tag\_list=, tag\_list.add etc…

> @page\_title = “Products tagged with ‘#{tag}’”  
> @products = Product.find\_tagged\_with(:any =\> tag, :separator =\>  
> ‘,’)  
> end
> 
> But when I try to view all other products with the same tag I get an  
> error:  
> You’ve just got a syntax error there - it should be @tag not tag
> 
> NameError in TagController#show  
> undefined local variable or method `tag’ for #\<TagController:  
> 0x2514c4c\>
> 
> Should I even have a tag controller?  
> The plugin docs use Product.find\_tagged\_with(‘tag’). How could I use  
> that?

Product.find\_tagged\_with @tag  
should work (assuming @tag contains a tag)

I’d encourage you to look closely at the docs, all i’ve done here is  
repeat them to you

Fred

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 16, 2008, 10:58am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/14 "2008-07-16T10:58:28Z")

</div>

Thanks Fred. That helped. I looked at a couple tutorials and none  
mentioned having a tag model (or the original plugin).  
One tutorial that was very helpful was:  
[http://dagobart.wordpress.com/2008/06/19/for-purpose-of-reference-enable-tagging-in-a-rails-model/](http://dagobart.wordpress.com/2008/06/19/for-purpose-of-reference-enable-tagging-in-a-rails-model/)  
Removing my tag model let’s me save the product 🙂

When I go to view product, I can see my assigned tag. But I still have  
questions/problems:

Q1. I’ve been having problems trying to update a product. My  
product\_controller.rb#update has:  
@product.tag(params[:tags], :separator =\> ‘,’, :clear =\> true)

When I try to update a product I get this error:  
NoMethodError in ProductController#update  
undefined method `tag’ for #Product:0x23e6dd4

Q2. To display the tags and links to show tags I use a helper method  
in application\_helper.rb:  
def display\_tags(product)  
product.tags.collect{|tag| link\_to tag.name, :controller =\> ‘/  
tag’, :action =\> ‘show’, :id =\> tag.name }.join(", ") if product.tags  
end

My tag\_controller.rb#show is:  
def show  
@tag = Tag.find(params[:id])  
@page\_title = “Products tagged with ‘#{tag}’”  
@products = Product.find\_tagged\_with(:any =\> tag, :separator =\>  
‘,’)  
end

But when I try to view all other products with the same tag I get an  
error:  
NameError in TagController#show  
undefined local variable or method `tag’ for #\<TagController:  
0x2514c4c\>

Should I even have a tag controller?  
The plugin docs use Product.find\_tagged\_with(‘tag’). How could I use  
that?

Thanks again so much,  
Elle

On Jul 16, 7:43 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 19, 2008, 10:50am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/15 "2008-07-19T10:50:08Z")

</div>

Sorry to be a bother and thank you so much for your help Fred. Almost  
all is fixed. Got one last problem/question.

On every product page, there is a section for recommended products –  
which really are products tagged with similar tags.  
In my catalog\_controller.rb#show I have:  
@tag = Tag.find(params[:id])  
@tagged\_related = Product.find\_tagged\_with (@tag, :match\_all =\>  
true)

But instead of picking on the tag’s id, it gets the product’s id. This  
causes two problems:

1. It doesn’t really show related products
2. If the id it gets doesn’t exists in the tags table, i get an error.  
I’m just not sure how to pick up the tag id instead.

Thanks,  
Elle

On Jul 16, 10:34 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [July 19, 2008, 1:23pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-foreign-keys/143000/16 "2008-07-19T13:23:06Z")

</div>

On Jul 19, 9:49 am, elle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. It doesn’t really show related products
> 2. If the id it gets doesn’t exists in the tags table, i get an error.  
> I’m just not sure how to pick up the tag id instead.

If problem is that params[:id] is a product id then surely the problem  
is just with whatever bit of code produces the link to this page?

Fred
