Acts_as_taggable and new taggable items


#1

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.


#2

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


#3

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.


#4

On 1/12/07, bjhess 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.
http://errtheblog.com


#5

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