Я работаю над довольно стандартной реализацией тегов для таблицы рецептов.Между рецептами и тегами существует много-много взаимосвязей, поэтому таблица тегов будет нормализована.Вот мои модели:
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
Последний бит, обратный вызов before_create, это то, что я пытаюсь заставить работать.Моя цель состоит в том, чтобы в случае попытки создать новый тег с тем же именем, что и имя в таблице, создается только одна строка в объединяемой таблице с использованием существующей строки в тегах.В настоящее время код умирает с:
undefined method `parent_type' for #<Class:0x102f5ce38>
Есть предложения?
Изменить
Вот и мои таблицы:
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