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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:18:35+00:00 2026-05-18T03:18:35+00:00

I have a HABTM-relation between the models Snippets and Tags. Currently, when i save

  • 0

I have a HABTM-relation between the models “Snippets” and “Tags”. Currently, when i save a snippet with a few tags, every tag is saved as a new record.

Now i want to check if a tag with the same name already exists and if that´s the case, i don´t want a new record, only an entry in snippets_tags to the existing record.

How can i do this?

snippet.rb:

class Snippet < ActiveRecord::Base
  accepts_nested_attributes_for :tags, :allow_destroy => true, :reject_if => lambda { |a| a.values.all?(&:blank?) }
  ...
end

_snippet.html.erb:

<% f.fields_for :tags do |tag_form| %>
  <span class="fields">
    <%= tag_form.text_field :name, :class => 'tag' %>
    <%= tag_form.hidden_field :_destroy %>
  </span>
<% end %>
  • 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-18T03:18:36+00:00Added an answer on May 18, 2026 at 3:18 am

    Ok, i´m impatient… after a while i found a solution that works for me. I don´t know if this is the best way, but i want to show it though.

    I had to modify the solution of Ryan Bates Railscast “Auto-Complete Association”, which handles a belongs_to-association to get it working with HABTM.

    In my snippet-form is a new text field named tag_names, which expects a comma-separated list of tags.

    Like Ryan, i use a virtual attribute to get and set the tags. I think the rest is self-explanatory, so here´s the code.

    View “_snippet.html.erb”

    <div class="float tags">
      <%= f.label :tag_names, "Tags" %>
      <%= f.text_field :tag_names %>
    </div>
    

    Model “snippet.rb”:

    def tag_names
      # Get all related Tags as comma-separated list
      tag_list = []
      tags.each do |tag|
        tag_list << tag.name
      end
      tag_list.join(', ')
    end
    
    def tag_names=(names)
      # Delete tag-relations
      self.tags.delete_all
    
      # Split comma-separated list
      names = names.split(', ')
    
      # Run through each tag
      names.each do |name|
        tag = Tag.find_by_name(name)
    
        if tag
          # If the tag already exists, create only join-model
          self.tags << tag
        else
          # New tag, save it and create join-model
          tag = self.tags.new(:name => name)
          if tag.save
            self.tags << tag
          end
        end
      end
    end
    

    This is just the basic code, not very well tested and in need of improvement, but it seemingly works and i´m happy to have a solution!

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

Sidebar

Related Questions

I have a HABTM relationship between Publications and Categories. In the new and edit
I have a HABTM relation and when i use the following code. the Multiple
From a practical and best practices perspective, should rails models that have HABTM association
I use CakePHP 1.2.6 and have the following relations: Showcase HABTM User belongsTo Galleryitem
So Rails doesn't have support for :through associations through a habtm relationship. There are
I have Event HABTM Category with categories_events table. I want to display only Event
I have two tables, Pages and Posts that are in HABTM and are joined
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution

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.