繁体   English   中英

委托类型 - Ruby on Rails

[英]Delegated types - Ruby on Rails

我仍在尝试为委托类型功能找到一个好的解决方案,因为我没有找到任何更好的更详细的指南,我想在这里问一下。

我感兴趣的是 DDH 所说的:

Delegated types. With this approach, the “superclass” is a concrete class that is represented by its own table, where all the superclass attributes that are shared amongst all the “subclasses” are stored. And then each of the subclasses have their own individual tables for additional attributes that are particular to their implementation.

在他们的示例中,我看到他们在超类上使用了创建者,并且是子类的共享属性,他们在评论控制器中使用了它,例如:

Entry.create! entryable: Comment.new(content: "Hello!"), creator: Current.user

在这种情况下, creator将始终相同,因此正确实现。

我的问题

(正如他们所提到的)如果您希望超类保留所有其他子类都可以使用的 title 属性(只是一个示例)怎么办,我该如何处理? 在这种情况下,标题是可变的(根据用户输入,每个子类的值都会不同),它应该如何在子类控制器上实现。

随机示例:

注意:ofc 我创建了 Productable 模块并包含在子类模型中

# schema.rb

ActiveRecord::Schema.define(version: 2021_08_23_191445) do

  create_table "laptops", force: :cascade do |t|
    t.integer "usb_ports"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "mobiles", force: :cascade do |t|
    t.integer "front_camera"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "Product", force: :cascade do |t|
    t.string "title"
    t.string "description"
    t.string "productable_type"
    t.integer "productable_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

end
# laptops_controller.rb

class LaptopsController < ApplicationController

  def new
    @laptop = Laptop.new
  end

  def create
    @laptop = Product.create! productable: Laptop.new(laptop_params) # Here to add what for the title?
    redirect_to @laptop
  end

  private

  def set_laptop
    @laptop = Laptop.find(params[:id])
  end

  def laptop_params
    params.require(:laptop).permit(:usb_ports)
  end
end
# _form.html.erb

<%= form_with(model: @laptop) do |form| %>

  <div class="field">
    <%= form.label :usb_ports %>
    <%= form.number_field :usb_ports %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>

<% end %>

我应该像多态关联一样使用嵌套属性吗? 有没有人有什么建议? 或者,在每个子类和属性上添加标题属性,在超类上仅添加永不改变的属性? 但是,这又有什么意义呢?

谢谢 :)

并在超类上只添加永远不会改变的属性

我认为您误读了文档。 直到今天我才知道这个特性(所以感谢你引起我的注意),但从我所见,使用这样的超类的目标不是通过重用/共享字段来节省空间所有记录都相同(如果它们对于所有记录确实相同,则它们不需要是字段。只需对它们进行硬编码或放入配置文件中)。

因此,如果您正在创建评论或消息,则每个新评论或消息都将创建自己的条目(本示例中的委托超类)。 条目不会被共享,因此可以自由地拥有可变字段。

是的,由于此功能似乎只是多态关联上的一层糖,您可以/应该使用所有相同的方法(嵌套属性等)

如果其他人面临同样的问题,当我寻找它时, accepts_nested_attributes_for选项对delegated_type不可用,因此我无法对通过delegated_type配置的关联对象使用嵌套表单。

但是: Rails 7添加了accepts_nested_attributes_for支持到delegated_type https://github.com/rails/rails/pull/41717 🎉

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM