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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:30:47+00:00 2026-06-02T12:30:47+00:00

My Tag model has some validations for attribute name . It’s wroking fine in

  • 0

My Tag model has some validations for attribute name. It’s wroking fine in all other cases. But when I call find_or_create_by_name by this way:

# The last value of this string (by coma) is empty.
# So this record should not be saved.
tags_line = 'ruby, javascript, '

tags_line.split(',').each do |tag_name|
  tag = Tag.find_or_create_by_name(tag_name.strip) do |new_tag|
    new_tag.update_attribute :user_id, member.user.id
  end

  # Just append tag to this model
  # through `has_and_belongs_to_many :tags`
  tags << tag if tag
end

But this empty tag is even being saved. So, can be something wrong in this code?

NOTE: When I remove the block, it works:

...
tags_line.split(',').each do |tag_name|
  # This way, invalid tag will not be created.
  tag = Tag.find_or_create_by_name(tag_name.strip)
  tags << tag if tag
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-06-02T12:30:49+00:00Added an answer on June 2, 2026 at 12:30 pm

    The problem was caused by the fact that attribute :name is protected in model. So, as the Rails Doc says:

    The same dynamic finder style can be used to create the object if it
    doesn’t already exist. This dynamic finder is called with
    find_or_create_by_ and will return the object if it already exists and
    otherwise creates it, then returns it. Protected attributes won’t be
    set unless they are given in a block.

    The fixed code is…

    tags_field.split(',').each do |tag_name|
      tag_name.strip!
    
      tag = General::Tag.find_or_create_by_name(tag_name) do |new_tag|
        # :name is attr_protected
        new_tag.name = tag_name
        new_tag.user_id = member.user.id
      end
    
      tags << tag if tag
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Entry model which has many Tag s. Tag s are added
I have model: # encoding: utf-8 class Tag include Mongoid::Document field :name, type: String
<script type=text/javascript> $(document).ready(function() { //select all the a tag with name equal to modal
I'm writing some tricky polymorphic relationships to handle tagging. I have a Tag model,
I have a problem. Imagine this data model: [Person] table has: PersonId, Name1 [Tag]
My database has the following schema: class Product(models.Model): pass class Tag(models.Model): product = models.ForeignKey(Product)
I wanted to add a StackOverflow-style tag input to a blog model of mine.
I am trying to save a model named 'customer' using form_for tag. I do
I am trying to design a tagging system with a model like this: Tag:
In tag#show I list all entries with that tag. At the bottom of 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.