Rails关联方法错误

时间:2015-10-01 17:57:55

标签: ruby-on-rails

在我的ruby on rails应用程序中,我已经应用了两个表之间的关系:文章和类别与关系has_and_belongs_to_many。

class Category < ActiveRecord::Base
   has_and_belongs_to_many :articles
end

class Article < ActiveRecord::Base
   has_and_belongs_to_many :categories
end

我正在按照本教程实现一个带有复选框(Railcast)的Has_many系统

我写了这一部分:

<% for category in Category.all%>
      <div>
        <%= check_box_tag "article[category_ids][]", category.id, @article.categories.include?(category) %>
        <%= category.name %>
      </div>
    <% end %>

但是我遇到了这个错误:

  

Mysql2 ::错误:表'CMS_development.articles_categories'不存在:显示来自articles_categories的完整字段

我哪里错了?

编辑添加移民和模式

迁移:

   class AddCategoryToArticles < ActiveRecord::Migration
      def change
        add_reference :articles, :category, index: true, foreign_key: true
      end
    end

SCHEMA:

    ActiveRecord::Schema.define(version: 20151001153131) do

  create_table "articles", force: :cascade do |t|
    t.boolean  "published"
    t.boolean  "on_evidance"
    t.boolean  "foreground"
    t.string   "title",       limit: 255
    t.string   "subtitle",    limit: 255
    t.datetime "date"
    t.text     "body",        limit: 65535
    t.text     "small_body",  limit: 65535
    t.datetime "created_at",                null: false
    t.datetime "updated_at",                null: false
    t.integer  "category_id", limit: 4
  end

  add_index "articles", ["category_id"], name: "index_articles_on_category_id", using: :btree

  create_table "categories", force: :cascade do |t|
    t.string   "name",        limit: 255
    t.text     "description", limit: 65535
    t.datetime "created_at",                null: false
    t.datetime "updated_at",                null: false
  end

  add_foreign_key "articles", "categories"
end

2 个答案:

答案 0 :(得分:1)

您应该编写并执行迁移以创建articles_categories table:

class CreateArticleCategory < ActiveRecord::Migration
  def change
    create_table :articles_categories do |t|
      t.references :articles
      t.references :categories
    end
  end
end

答案 1 :(得分:1)

两个问题:

  1. 您没有加入表articles_categories
  2. 您没有正确引用categories
  3. 首先,要使用has_and_belongs_to_many关联,您需要调用相应的连接表

    enter image description here

    您的架构非常清楚地表明您没有拥有此功能。如其他答案所述,您需要按如下方式创建架构:

       class CreateArticlesCategories < ActiveRecord::Migration
          def change
              create_table :articles_categories, id: false  do |t|
                 t.references :articles
                 t.references :categories
              end
          end
        end
    

    这将为您提供连接表,您可以使用选择框填充...

    -

    对于您的复选框,您可以执行以下操作:

    #app/views/articles/new.html.erb
    <%= form_for @article do |f| %>
       <%= f.collection_select :categories, Category.all, :id, :name %>
       <%= f.submit %>
    <% end %>
    

    要通过控制器填充此内容,您需要使用以下内容:

    #app/controllers/articles_controller.rb
    class ArticlesController < ApplicationController
       def new
          @article = Article.new
       end
       def create
          @article = Article.new article_params
          @article.save
       end
    
       private
    
       def article_params
          params.require(:article).permit(:title, :categories)
       end
    end