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

  • Home
  • SEARCH
  • 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 3614980
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:19:00+00:00 2026-05-18T22:19:00+00:00

I am working on a pretty standard tagging implementation for a table of recipes.

  • 0

I am working on a pretty standard tagging implementation for a table of recipes. There is a many to many relationship between recipes and tags so the tags table will be normalized. Here are my models:

class Recipe < ActiveRecord::Base
    has_many :tag_joins, :as => :parent
    has_many :tags, :through => :tag_joins
end

class TagJoin < ActiveRecord::Base
    belongs_to :parent, :polymorphic => true
    belongs_to :tag, :counter_cache => :usage_count
end

class Tag < ActiveRecord::Base
    has_many :tag_joins, :as => :parent
    has_many :recipes, :through => :tag_joins, :source => :parent
        , :source_type => 'Recipe'


    before_create :normalizeTable
    def normalizeTable
        t = Tag.find_by_name(self.name)
        if (t) 
            j = TagJoin.new
            j.parent_type = self.tag_joins.parent_type
            j.parent_id = self.tag_joins.parent_id
            j.tag_id = t.id
            return false
        end
    end
end

The last bit, the before_create callback, is what I’m trying to get working. My goal is if there is an attempt to create a new tag with the same name as one already in the table, only a single row in the join table is produced, using the existing row in tags. Currently the code dies with:

undefined method `parent_type' for #<Class:0x102f5ce38>

Any suggestions?

Edit

Here are my tables as well:

create_table "recipes", :force => true do |t|
    t.string   "name"
    t.text     "abstract",   :limit => 255
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
end

create_table "tag_joins", :force => true do |t|
    t.string   "parent_type"
    t.integer  "parent_id"
    t.integer  "tag_id"
    t.datetime "created_at"
    t.datetime "updated_at"
end

create_table "tags", :force => true do |t|
    t.string   "name"
    t.boolean  "is_featured"
    t.integer  "usage_count"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "category"
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-18T22:19:00+00:00Added an answer on May 18, 2026 at 10:19 pm

    I don’t see how this can work. If you add a tag through the association, the tag is created before the join table row. Also, the callback is trying to get the parent_type of a collection, which makes no sense.

    I’m assuming you want something like this:

    r1 = Recipe.create
    r1.tags.create("chili") # tag is created
    
    r2 = Recipe.create
    r2.tags.create("chili") # existing tag is used
    

    You could emulate this by defining a method on Recipe:

      def add_tag(tag_name)
        tags << Tag.find_or_create_by_name(tag_name)
      end
    

    And calling it instead of .tags.create:

    r1 = Recipe.create
    r1.add_tag("chili") # tag is created
    
    r2 = Recipe.create
    r2.add_tag("chili") # existing tag is used
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is pretty standard stuff here, and I cannot understand why it isn't working.
The app I'm working with is well established and uses a pretty standard CakePHP
I'm working on what I think is a pretty standard django site, but am
We have pretty standard web-site and I got a task to convert links between
I've got an app that's working pretty flawlessly in Chrome and FF, however, when
I am working on a pretty simple web application (famous last words) and am
I'm working on a pretty simple Java app in order to learn more about
I'm working on a pretty complex web application in Ext with Google maps API
I'm working on some middleware for rails, working with a pretty recent version: pfernand-2-mn:~
I have a very strange situation. I´m working on a pretty big Java application

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.