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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:48:46+00:00 2026-05-12T00:48:46+00:00

I have an Entry model which has many Tag s. Tag s are added

  • 0

I have an Entry model which has many Tags. Tags are added to an entry by typing them into a textbox on my form, via a tag_names virtual attribute. Before validation on the Entry model, the tag_names string is converted into actual Tag objects using find_or_create_by_name. The Tag model also has validation to make sure the tag name matches a regex, which is run via the association.

My Entry model looks like this:

class Entry < ActiveRecord::Base
  has_many :entry_tags
  has_many :tags, :through => :entry_tags

  before_validation :update_tags

  attr_writer :tag_names

private
  def update_tags
    if @tag_names
      self.tags = @tag_names.split(",").uniq.map do |name|
        Tag.find_or_create_by_name(name.strip)
      end
    end
  end
end

When I create a new Entry object and assign it tags, everything works correctly — the tags are not saved if there is a validation error on one of the Tags, and an error message is passed back. However, if I try to update an existing Entry object with an invalid tag, instead of passing back a message, my self.tags= call (in update_tags above) is throwing an exception with the validation error message. Even if I overwrite find_or_create_by_name to actually just return a new object instead of calling create, I get the same outcome.

It seems to me (and the docs seem to corroborate) that the tags= call is actually saving my Tag objects before the main record gets saved when the Entry object already exists. Is there anything I can do to make this save not happen, or to stop it from raising an exception and just causing my save to return false?

  • 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-12T00:48:46+00:00Added an answer on May 12, 2026 at 12:48 am

    I would try something like this:

    class Entry < ActiveRecord::Base
      has_many :entry_tags
      has_many :tags, :through => :entry_tags
    
      before_validation :update_tags
    
      attr_writer :tag_names
      validates_associated :tags
    
    private
      def update_tags
        return unless @tag_names
        current_tag_names = tags.map(&:name)
        user_tag_names = @tag_names.split(",").uniq
        #add in new tags
        user_tag_names.each do |name|
          next if current_tag_names.include?(name)
          tags.build :name => name
        end
        #remove dropped tags
        ( current_tag_names - user_tag_names ).each do |name|
          removed_tag = tags.find_by_name(name)
          tags.delete(removed_tag)
        end
      end
    end
    

    This way you’re only initializing the related models in your update_tags action and so won’t throw validation errors. I also added in the validates_associated :tags so that errors on these related models can be reported back via the standard input form using error_messages_for :entry.

    Update included code for removing dropped tags.

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

Sidebar

Related Questions

I have a single entry output from a paradox table which is imported into
I have a model which has a field status definde as: class Model(models.Model): ...
I am trying to use a textarea form entry which I have some javascript
Suppose I have a model user , which has a uniqueness constraint on the
I have an entry.rb model and I'm trying to make a semi-complicated validation. I
I have a such model named Foo: class Foo(models.Model): name = models.CharField() entry =
I have an entity ( TerminalCertification ) which has relation to other entities. I
I have a model called Tournament, which has_many Entries. There is a method in
I see many MVC implementations for websites have a single-entry point such as an
I have an articles entry model and I have an excerpt and description field.

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.