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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:20:33+00:00 2026-05-30T18:20:33+00:00

I have a Tag model with only one attribute: :name . tag.rb: class Tag

  • 0

I have a Tag model with only one attribute: :name.

tag.rb:

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

(it has a many-to-many relationship with a Post model):

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

  validates :title,   :presence => true,
                      :length   => { :maximum => 30 },
                      :uniqueness => true
  validates :content, :presence => true,
                      :uniqueness => true

  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
    if @tag_names
      self.tags = @tag_names.split(" ").map do |name|
        Tag.find_or_create_by_name(name)
      end
    end
  end
end

As you can imagine I don’t want to have 2 tags with the same name. Right now, it is possible to create them. If there is a tag like this: <Tag id=1, name=food> and later another tag like this is created: <Tag id=2, name=food>. I want the second to be ignored because there is already a tag with the name food.

(Pretty much how all tag systems works)

Any suggestions in order to accomplish this?

  • 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-30T18:20:34+00:00Added an answer on May 30, 2026 at 6:20 pm

    I am not sure if I completely understand your problem but maybe this will be helpful for you.
    …

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

Sidebar

Related Questions

Consider I have defined the following models: class Tag(models.Model): name = models.CharField(max_length=20) class Entry(models.Model):
I have this simple model: class Tag(models.Model): title = models.SlugField() created = models.datetime def
I have the following models: class Mark < ActiveRecord::Base validates_presence_of :user validates_presence_of :page belongs_to
I have two models as follows: class Tag(models.Model): # ... class Paragraph(models.Model): tags =
so i have got two models with each one controller: Model Project has_many Themes
I have a pretty generic Article model, with m2m relation to Tag model. I
I have an Entry model which has many Tag s. Tag s are added
I have a problem. Imagine this data model: [Person] table has: PersonId, Name1 [Tag]
i have this tag as input tag: <a href=controller.jsp?sid=127490C88DB5&R=35144 class=11-link-dkred-bold><b>Mr. John Q. Anderson&nbsp;&nbsp;&nbsp;MBA 1977
I have a Solutions model, with has_many Reviews, and Reviews with 'has_many' ReviewValues. Each

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.