Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7027341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:14:05+00:00 2026-05-28T00:14:05+00:00

I am trying to implement a rails tagging model as outlined in Ryan Bate’s

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:14:05+00:00Added an answer on May 28, 2026 at 12:14 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I went to this tutorial http://railscasts.com/episodes/221-subdomains-in-rails-3 and trying to implement in http://ngtv2.info but after
I am trying to implement a rails Recipe application from the site http://oreilly.com/ruby/archive/rails-revisited.html .
As part of learning ruby/rails, I'm trying to implement http://github.com/professionalnerd/simple-private-messages into my application from
I've been trying to implement a dynamic multi-model form with accepts_nested_attributes_for in my rails
I am trying to implement a tagging system for my rails app. I employed
I'm trying to implement the rails-settings gem (https://github.com/100hz/rails-settings) into my Rails 3 project using
I am trying to implement a bulk update feature in a Rails app, the
I'm trying to implement thinking-sphinx across multiple 'sites' hosted under a single rails application.
I'm trying to implement a has_many :through join in Rails 3 (with Formtastic) and
I'm trying to implement a comet approach in my Rails application. I have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.