ruby-on-rails - 具有多态关联的未知属性

标签 ruby-on-rails ruby polymorphism

在我的网上商店中,我有表ProductSize,我想我还需要添加一个表Restocking

与其更新产品,我想最好有一个补货表,这样我就可以跟踪我添加任何新尺寸、数量以及为什么不添加新价格(买卖)的日期)... 并创建统计信息...

你认为这是正确的吗?

创建补货后,相应的产品会更新为新的数量和价格吗?

嗯,

所以它是这样开始的:

#Product 
has_many :sizes
accepts_nested_attributes_for :sizes, reject_if: :all_blank, allow_destroy: true

#Size 
belongs_to :product

补货表需要有尺寸属性(比如产品)

我相信我必须使用多态关联,但我应该如何更新我的架构,我应该添加、删除什么?

因此,由于我添加了Restocking 模型,我的模型如下所示:

#Product
has_many :sizes, inverse_of: :product,  dependent: :destroy, as: :sizeable
has_many :restockings
accepts_nested_attributes_for :sizes, reject_if: :all_blank, allow_destroy: true

#Restocking
has_many :sizes, as: :sizeable
belongs_to :product
accepts_nested_attributes_for :sizes, reject_if: :all_blank, allow_destroy: true


#Size
belongs_to :product
belongs_to :restocking
belongs_to :sizeable, polymorphic: true, class_name: "Size"

架构.rb

 create_table "sizes", force: :cascade do |t|
    t.string "size_name"
    t.integer "quantity"
    t.bigint "product_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "quantity_stock"
    t.index ["product_id"], name: "index_sizes_on_product_id"
  end


  create_table "restockings", force: :cascade do |t|
    t.bigint "product_id"
    t.bigint "sizeable_id"
    t.decimal "price", precision: 10, scale: 2
    t.decimal "buying_price", precision: 10, scale: 2
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["product_id"], name: "index_restockings_on_product_id"
    t.index ["sizeable_id"], name: "index_restockings_on_sizeable_id"
  end

  create_table "products", force: :cascade do |t|
    t.string "title", limit: 150, null: false
    t.text "description"
    t.bigint "category_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "color"
    t.integer "user_id"
    t.json "attachments"
    t.string "brand"
    t.string "ref"
    t.decimal "price"
    t.decimal "buying_price", precision: 10, scale: 2
    t.index ["category_id"], name: "index_products_on_category_id"
  end

此时我有几个错误,比如

在产品 Controller 中

    def new
      @product = Product.new
      @product.sizes.build
    end

错误:

ActiveModel::UnknownAttributeError at /admin/products/new
unknown attribute 'sizeable_id' for Size.

您能告诉我必须更改的迁移吗? 欢迎提出建议

最佳答案

你几乎就在那里,要在你的 Size 模型中使用多态,你必须更改 size 资源,并向资源添加两个属性: sizeable_idsizeable_type

sizeable_type是一个字符串,表示父元素的类,在你的例子中,可以是ProductRestocking,并且sizeable_id 表示找到父元素的element_id,你的关系是正确的,但是你必须把这个元素添加到你的Size,见下面:

迁移到您的案例的一个示例:

class AddSizeableToSize < ActiveRecord::Migration
  def change
    add_reference :sizes, :sizeable, polymorphic: true, index: true
  end
end

在您的Size 模型上:

# app/models/size.rb

class Size < ActiveRecord::Base
  belongs_to :sizeable, polymorphic: true
end

在您的ProductRestocking 模型中:

has_many :sizes, as: :sizeable

这只是让您的案例发挥作用的简单方法!如果你想了解更多关于rails associations和polymorphism,可以看看这个link .

关于ruby-on-rails - 具有多态关联的未知属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54832831/

相关文章:

ruby-on-rails - Rails 4 I18n-js gem 没有获取语言环境文件的更改

java - 我对 Java 中的多态性有疑问

java - Python 多态 VS JAVA 多态

ruby-on-rails - 如何检查一个国家是否属于欧洲

ruby-on-rails - 如何在 heroku 上使用本地 gem?

ruby-on-rails - ruby,为什么在模块中使用模块?

mysql - ruby MySQL 返回#<Mysql :0x000000016f3780> and not nil

mysql - Rails 中的 after_create 使用手动 SQL 查询

ruby-on-rails - 使用 Net::HTTP 时 HTTP 请求路径为空

c++ - 使用模板的动态多态行为?