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 7585421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:10:19+00:00 2026-05-30T19:10:19+00:00

I have a Tag model: class Tag < ActiveRecord::Base has_many :taggings, :dependent => :destroy

  • 0

I have a Tag model:

class Tag < ActiveRecord::Base
  has_many :taggings, :dependent => :destroy  
  has_many :posts, :through => :taggings
  has_many :subscriptions
  has_many :subscribed_users, :source => :user, :through => :subscriptions
end

and a Post model:

class Post < ActiveRecord::Base
  attr_accessible :title, :content, :tag_names

  belongs_to :user

  has_many :taggings, :dependent => :destroy
  has_many :tags, :through => :taggings

  attr_writer :tag_names
  after_save :assign_tags

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

  private

  def assign_tags
    return if @tag_names.blank?
    @tag_names.split(" ").each do |name|
      tag = Tag.find_or_create_by_name(name)
      self.tags << tag unless tags.include?(tag)
    end
  end
end

The assign_tags method stores the saved tags in :tag_names and displays it in an input field in this fashion: tag1 tag2 tag3 (values are separated by spaces).

views/posts/_form.html.erb:

  <div class="field">
    <%= f.label :tag_names %><br />  
    <%= f.autocomplete_field :tag_names, autocomplete_tag_name_posts_path, :"data-delimiter" => ' '%>
  </div>

As you can see, assign_tags, enables me to add/push tags to tag_names. But if I delete a tag from the input field and save. The tag will still show up.

Any suggestions to help me solve this issue?

(Maybe tag_names should just empty itself before the tags are pushed in. But no idea how to code that).

  • 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-30T19:10:21+00:00Added an answer on May 30, 2026 at 7:10 pm

    Before assigning tags, you’ll want to clear the tags for that item. e.g.

    def assign_tags
      self.tags = []
      return if @tag_names.blank?
      @tag_names.split(" ").each do |name|
        tag = Tag.find_or_create_by_name(name)
        self.tags << tag unless tags.include?(tag)
      end
    end
    

    Update:

    Or, a method that will hit the database a bit less:

    def assign_tags
      ntags = []
      @tag_names.to_s.split(" ").each do |name|
        ntags << Tag.find_or_create_by_name(name)
      end
      self.tags = ntags
    end
    

    If you wanted to get really crazy with the write-less-do-more paradigm, you could do:

    def assign_tags
      self.tags = @tag_names.to_s.split(" ").inject([]) do |arr, name|
        arr << Tag.find_or_create_by_name(name)
        arr
      end
    end
    

    Alternatively, I’d recommend looking into the acts-as-taggable-on gem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Post model: class Post < ActiveRecord::Base belongs_to :user has_many :taggings, :dependent
I have a Post model: class Post < ActiveRecord::Base belongs_to :user has_many :taggings, :dependent
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names has_many
I have a model: class EvidenceType < ActiveRecord::Base has_many :evidences attr_accessible :name end A
I have the following models class Outbreak < ActiveRecord::Base has_many :risks has_many :factors, :through
I have the following models: class Keyword < ActiveRecord::Base has_many :tags has_many :studies, :through
I have a pretty simple HABTM set of models class Tag < ActiveRecord::Base has_and_belongs_to_many
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to
how do i have make it working? class Device < ActiveRecord::Base has_many :prices has_many
I have a model: class Topic < ActiveRecord::Base define_index do indexes title, :sortable =>

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.