# Acts\_as\_taggable and new taggable items

**URL:** <https://www.ruby-forum.com/t/acts-as-taggable-and-new-taggable-items/82611>\
**Category:** Rails\
**Created:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-new-taggable-items/82611 "2007-01-19T16:31:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjhess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@bjhess](https://www.ruby-forum.com/u/bjhess)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-new-taggable-items/82611/1 "2007-01-19T16:31:40Z")

</div>

I just brought acts\_as\_taggable into an app and I have one question.  
Is the following the best way to tag a brand new taggable item? (Note:  
I’m grabbing the data from a form - I cut the code down to its basics  
for the post.)

def save\_story  
story = Story.new(params[:story])  
story.save  
story.tag\_with(params[:story][:tag\_list])  
story.save  
end

Near as I can tell, it’s not possible to successfully tag a taggable  
item until the item has an id. Saving the item once before tagging  
will generate that id for me, allowing me to save the item a second  
time, which registers the tagging information.

---

<div class="post-metadata">

**Author:** ![bjhess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@bjhess](https://www.ruby-forum.com/u/bjhess)\
**Post date:** [January 21, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-new-taggable-items/82611/2 "2007-01-21T21:23:54Z")

</div>

Thought I’d ping the group. Anyone with solid acts\_as\_taggable  
experience who can tell me a better way to do the above?

---

<div class="post-metadata">

**Author:** ![bjhess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@bjhess](https://www.ruby-forum.com/u/bjhess)\
**Post date:** [January 22, 2007, 1:01am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-new-taggable-items/82611/3 "2007-01-22T01:01:30Z")

</div>

I do not believe there is a better way to do this. The id must be  
created  
first to be stored in the taggable join table.

---

<div class="post-metadata">

**Author:** ![bjhess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@bjhess](https://www.ruby-forum.com/u/bjhess)\
**Post date:** [January 22, 2007, 1:15am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-new-taggable-items/82611/4 "2007-01-22T01:15:31Z")

</div>

On 1/12/07, bjhess [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> Try keeping your controller skinny by adding accessors and callbacks  
> to your model.

class Story \< ActiveRecord::Base  
after\_save :tagify  
attr\_accessor :tag\_list

def tagify  
self.tag\_list = tag\_list.split(‘,’) if tag\_list.is\_a? String #  
comma, or whatever  
tag\_list.each { |tag| tag\_with(tag) }  
end  
end

Now, in your controller:

def save\_story  
Story.create(params[:story])  
end

–  
Chris W.

> **[err.the\_blog](http://errtheblog.com)**
>
> Err the Blog: Ruby and Rails from the front.

---

<div class="post-metadata">

**Author:** ![bjhess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@bjhess](https://www.ruby-forum.com/u/bjhess)\
**Post date:** [January 22, 2007, 11:33am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-new-taggable-items/82611/5 "2007-01-22T11:33:59Z")

</div>

Ask and you shall receive - check this out:  
[http://blog.hasmanythrough.com/2007/1/22/using-faux-accessors-to-initialize-values](http://blog.hasmanythrough.com/2007/1/22/using-faux-accessors-to-initialize-values)
