0

我正在做一個簡單的待辦事項應用程序,我只有待辦事項和標記模型以及它們之間的has_many_belongs_to_many關係。我還設置了爲加入它看起來像下面這樣的todo_tags遷移:Rails混合連接表名中有很多屬於很多

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 

但是,當我嘗試刪除任何標籤或待辦事項我收到以下錯誤eventhough我沒有設置任何標籤之間的任何連接待辦事項呢。

Mysql2::Error: Table 'wa2do.tags_todos' doesn't exist: SELECT tags .* FROM tags INNER JOIN tags_todos ON tags . id = tags_todos . tag_id WHERE tags_todos . todo_id = 298486374

我不知道在哪裏的Rails得到tags_todos表的想法,當我遷移表中調用todo_tags。實際上這是一個練習,他們要求使用這個名字。

我有兩個型號,他們是這樣的: 的Todo類 藤堂<的ActiveRecord :: Base的 attr_accessible:TASK_NAME,:DUE_DATE,:完了,:優先

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 

和標籤 class 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 

回答

0

我發現一些可能是正確的答案。在這種情況下,我想我只能通過使用一種有許多直通關係的方式來扭轉這種局面,而我並不需要這種關係。

note that the table needs to by named in alphabetical order i.e. categories_stories table as opposed to stories_categories - this is the convention that makes it work.