【问题标题】:Can't associate product and category correctly无法正确关联产品和类别
【发布时间】:2015-12-04 20:41:58
【问题描述】:

大家。我试图将每个产品与他的类别联系起来。但是有一些错误。

所以,一开始。 这是我的产品和类别迁移:

class CreateProducts < ActiveRecord::Migration
   def change
      create_table :products do |t|
          t.belongs_to :categoty, index: true
          t.string :title
          t.string :category
          t.text :description
          t.string :image_url
          t.decimal :price, precision: 8, scale: 2
          t.timestamps
      end
   end
end


class CreateCategories < ActiveRecord::Migration
  def change
    create_table :categories do |t|
      t.references :product
      t.string :name
      t.timestamps null: false
    end
  end
end

它们在模型中都有关联(belongs_to 用于产品,has_many 用于类别)。

我已经制作了将产品与此类别相关联的表格:

<div class="field">
  <%= f.collection_select :category, Category.all, :id, :name %>
</div>

当我尝试保存产品时出现错误: 应为类别(#XXXX),得到字符串(#XXXX)。

那么,我做错了什么?

另外,这是我的控制器。

class ProductsController < ApplicationController


    before_action :set_product, only: [:show, :edit, :update, :destroy]


      # GET /products
      # GET /products.json
      def index
        @products = Product.all
      end

      # GET /products/1
      # GET /products/1.json
      def show
      end

      # GET /products/new
      def new
        @product = Product.new
      end

      # GET /products/1/edit
      def edit
      end

      # POST /products
      # POST /products.json
      def create
        @product = Product.new(product_params)

        respond_to do |format|
          if @product.save
            format.html { redirect_to @product, notice: 'Product was successfully created.' }
            format.json { render :show, status: :created, location: @product }
          else
            format.html { render :new }
            format.json { render json: @product.errors, status: :unprocessable_entity }
          end
        end
      end

      # PATCH/PUT /products/1
      # PATCH/PUT /products/1.json
      def update
        respond_to do |format|
          if @product.update(product_params)
            format.html { redirect_to @product, notice: 'Product was successfully updated.' }
            format.json { render :show, status: :ok, location: @product }
          else
            format.html { render :edit }
            format.json { render json: @product.errors, status: :unprocessable_entity }
          end
        end
      end

      # DELETE /products/1
      # DELETE /products/1.json
      def destroy
        @product.destroy
        respond_to do |format|
          format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
          format.json { head :no_content }
        end
      end

      private
        # Use callbacks to share common setup or constraints between actions.
        def set_product
          @product = Product.find(params[:id])
        end

        # Never trust parameters from the scary internet, only allow the white list through.
        def product_params
          params.require(:product).permit(:title, :category, :description, :image_url, :price)
        end
    end

【问题讨论】:

  • belongs_to :category 将在产品表中添加 category_id,这将成功建立类别 has_many :products 关系。我们不需要类别表中的references :product
  • 感谢您的关注!现在我知道。但我仍然有一个错误。
  • 请分享您的控制器操作以清楚了解您如何调用 save
  • 您在Product 模型中有String 类型的:category 属性。如果还有Category模型相关,真的有必要吗?可能是引发异常的原因。
  • @Mareq 是对的。将其设为 category_id &lt;%= f.collection_select :category_id, Category.all, :id, :name %&gt;。并将其添加到params.require(:product).permit(:title, :category_id,.... 中。

标签: ruby-on-rails ruby


【解决方案1】:

在您的数据库迁移文件中,您有字段categoty,并且您在控制器中使用category_id。只需要将数据库中的字段更改为category,以确保它与正确的模型相关联。

【讨论】:

  • 真的!有用!抱歉.... buuuuuuut 当我试图显示时还有另一个问题 出现错误“没有这样的列:categories.product_id: SELECT "categories".* FROM "类别” WHERE “类别”。“product_id” = ?所以我忘了添加什么?
  • 您能否将您的代码放入这些模型中,以便我可以帮助您检查关联问题?顺便说一句,我认为如果这两个模型之间只有1-N 关系,您不必在categories 表中添加product_id
  • 当然。分类型号:class Category &lt; ActiveRecord::Base has_many :products, dependent: :destroy end 产品代码:class Product &lt; ActiveRecord::Base has_one :category end
  • 我认为在您的 Product 模型中将其更改为 belongs_to :category 会修复它。
  • 真的!感谢您回答我的愚蠢问题!我永远记得你的好意!
猜你喜欢
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 1970-01-01
相关资源
最近更新 更多