Hi there,
I am trying to create some sort of a blog and i'm trying to create is a method that takes care of the creation/removal of the records in the join table.
So i have a blog that has many "tags" (join table)
I will let the code speak for itself.
#technology model has_many :tags, :dependent => :destroy has_many :blogs, :through => :tags
#Tag model belongs_to :technology belongs_to :blog
#blog model has_many :tags, :dependent => :destroy has_many :technologies, :through => :tags
def tagging=(tagging) tags.each do |tag| tag.destroy unless tagging.include? (tag.blog_id.to_i) end tagging.each do |tag| self.tags.create(:technology_id => tag) unless tags.any?{|t| t.technology_id.to_i == tag.to_i} end end
#blog controller def create params[:blog][:tagging] ||= ... end
#blog form <div id="technology_form"> <font class=blog_technologies>Blog technologies</font> <p> <% for technology in @technologies %> <%= check_box_tag "blog[tagging]", technology.id, @blog.technologies.include?(technology) %> <%= technology.name %> <br /> <% end %> </p> </div> ....
I know i have to write the tagging method that will create or delete the "tags", and it's working only when im creating a tag, but when i delete it shows an error:
Mysql::Error: #42S22Unknown column 'id' in 'where clause': DELETE FROM `tags` WHERE `id` = NULL
I know i don't have an id for the join table
class CreateTags < ActiveRecord::Migration def self.up create_table :tags, :id => false do |t| t.integer :blog_id t.integer :technology_id ...... end end
How can i solve this problem? thanks in advanced