1

I am trying to implement a rails tagging model as outlined in Ryan Bate's railscast #167. http://railscasts.com/episodes/167-more-on-virtual-attributes

This is a great system to use. However, I cannot get the form to submit the tag_names to the controller. The definition for tag_names is :

 def tag_names
   @tag_names || tags.map(&:name).join(' ')
 end

Unfortunately, @tag_names never gets assigned on form submission in my case. I cannot figure out why. SO it always defaults to tags.map(&:name).join(' '). This means that I can't create Articles because their tag_names are not there, and I also can't edit these tags on existing ones. Anyone can help?

jay
  • 12,066
  • 16
  • 64
  • 103

1 Answers1

1

In short, your class is missing a setter (or in Ruby lingo, an attribute writer). There are two ways in which you can define a setter and handle converting the string of space-separated tag names into Tag objects and persist them in the database.

Solution 1 (Ryan's solution)

In your class, define your setter using Ruby's attr_writer method and convert the string of tag names (e.g. "tag1 tag2 tag3") to Tag objects and save them in the database in an after save callback. You will also need a getter that converts the array of Tag object for the article into a string representation in which tags are separated by spaces:

class Article << ActiveRecord::Base
  # here we are delcaring the setter
  attr_writer :tag_names

  # here we are asking rails to run the assign_tags method after
  # we save the Article
  after_save :assign_tags

  def tag_names
    @tag_names || tags.map(&:name).join(' ')
  end

  private

  def assign_tags
    if @tag_names
      self.tags = @tag_names.split(/\s+/).map do |name|
        Tag.find_or_create_by_name(name)
      end
    end
  end
end

Solution 2: Converting the string of tag names to Tag objects in the setter

class Article << ActiveRecord::Base
  # notice that we are no longer using the after save callback
  # instead, using :autosave => true, we are asking Rails to save
  # the tags for this article when we save the article
  has_many :tags, :through => :taggings, :autosave => true

  # notice that we are no longer using attr_writer
  # and instead we are providing our own setter
  def tag_names=(names)
     self.tags.clear
     names.split(/\s+/).each do |name|
       self.tags.build(:name => name)
     end
  end

  def tag_names
    tags.map(&:name).join(' ')
  end
end
Behrang
  • 46,888
  • 25
  • 118
  • 160
  • hmm well i tried adding def tag_names=(value); @tag_names=value; end but this didn't work. I do not know what else to put in that space, what do you think? – jay Jan 07 '12 at 19:50
  • That semicolon is unnecessary. – Behrang Jan 07 '12 at 19:54
  • By the way, as a rule of thumb, semicolons in Ruby are optional... :) – Behrang Jan 07 '12 at 19:57
  • Behrang, I am a little confused by this setter. I have to define this setter in addition to the assign_tags method? why is there so much overlap in the setter and assign_tags – jay Jan 07 '12 at 20:14
  • @jay Jay, the setter converts the string into `Tag` objects, and the getter converts an array of `Tag` objects into a string. Let me update my answer and add more details. – Behrang Jan 07 '12 at 20:17
  • hey behrang, i like your alternate solution. I will try to implement it. I was going to ask, however, in the setter is it best practice to clear all tags, then build new ones each time - or to build a setter that compares new and old tag lists and saves/destroys based off that. Your solution (the former) might have fewer queries into the db? but it still costs some in clearing current tags. Thoughts? Thanks for your wonderful help! – jay Jan 07 '12 at 22:55
  • hey behrang - I am still having difficulty with your alternative solution. It appears that it won't put the entry from :tag_names in the form. Any thoughts? I used exactly your code for the second solution – jay Jan 08 '12 at 01:14
  • hey behrang - i really appreciate the help. I found a solution, you can add to the above that instead of @tag_list i found that read_attribute(:tag_list) is necessary in the getter, and then write_attribute(:tag_list, val) for the setter. If you add this, I'll more than happily award you the answer (and thanks again for all the attentive, wonderful assistance). – jay Jan 08 '12 at 05:46