Forum: Ruby on Rails acts_as_taggable and new taggable items

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
bjhess (Guest)
on 2007-01-19 17:31
(Received via mailing list)
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.
bjhess (Guest)
on 2007-01-21 22:23
(Received via mailing list)
Thought I'd ping the group.  Anyone with solid acts_as_taggable
experience who can tell me a better way to do the above?
Nicholas M. (Guest)
on 2007-01-22 02:01
(Received via mailing list)
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.
Chris W. (Guest)
on 2007-01-22 02:15
(Received via mailing list)
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
askegg (Guest)
on 2007-01-22 12:33
(Received via mailing list)
Ask and you shall receive - check this out:
http://blog.hasmanythrough.com/2007/1/22/using-fau...
This topic is locked and can not be replied to.