0

我正在做一个简单的待办事项应用程序,其中我只有待办事项和标记模型以及它们之间的 has_many_belongs_to_many 关系。我还为联接设置了 todo_tags 迁移,如下所示:

class CreateTodoTagsJoinTable < ActiveRecord::Migration
 def up
  create_table :todo_tags, :id => false do |t|
    t.integer :todo_id
    t.integer :tag_id
  end

    add_index :todo_tags, [:todo_id, :tag_id]
  end

  def down
   drop_table :todo_tags
  end
 end

但是当我尝试删除任何标签或待办事项时,即使我尚未在任何标签和待办事项之间建立任何连接,我也会收到以下错误。

Mysql2::Error: 表 'wa2do.tags_todos' 不存在:SELECT tags.* FROM tagsINNER JOIN tags_todosON tagsid= tags_todostag_id在哪里tags_todostodo_id= 298486374

我不知道 Rails 从哪里得到 tags_todos 表的想法,在迁移时我的表称为 todo_tags。实际上这是一个练习,他们要求使用这个名字。

我有两个模型,它们看起来像这样: Todo class Todo < ActiveRecord::Base attr_accessible :task_name, :due_date, :finished, :priority

 validate :task_name, :presence => true
 validate :due_date_in_future?

 has_and_belongs_to_many :tags

 def due_date_in_future?
  due_date > DateTime.current
 end

结尾

和标签类 Tag < ActiveRecord::Base attr_accessible :tag_name

  has_and_belongs_to_many :todos

  validates_uniqueness_of :tag_name, :on => :create, :message => "Tag name must be unique"

end

和我的控制器

def destroy
  @todo = Todo.find(params[:id])
  @todo.destroy

  respond_to do |format|
    format.html { redirect_to todos_url }
    format.json { head :no_content }
  end
end
4

1 回答 1

0

我发现了一些可能是正确答案的东西。在那种情况下,我想我只能通过使用我不需要的有很多直通关系来扭转局面。

请注意,该表需要按字母顺序命名,即 categories_stories 表而不是 stories_categories - 这是使其工作的约定。

于 2013-03-24T11:18:58.023 回答