在 Rails 中编辑关联的最简单方法

发布于 2024-12-26 06:22:33 字数 923 浏览 2 评论 0原文

我有我想象的一定是常见情况,但可能找不到表达问题以获得解决方案的方法...

我的应用程序中有几个链接的模型:

class Product < ActiveRecord::Base
  validates_uniqueness_of :prod_code
end

class Stock < ActiveRecord::Base
  belongs_to :product
end

实际上还有许多其他模型这也属于产品。默认情况下,在库存记录中,我只看到product_id字段,这是一个自动递增的数字,对用户没有太大帮助。产品有一个唯一的prod_code,它位于条形码等上,是产品数据库的自然密钥。

我希望库存和其他链接模型的创建/编辑屏幕显示 prod_code 的文本字段,并能够响应 stock[prod_code] 形式的参数一种明智的方式(例如查找 prod_code,并根据结果设置 prod_id),并且自动(例如 Stock.new(params[:stock]) 应该可以工作。

为了澄清,设置stock[prod_code] 不会更改产品数据库中的任何内容;它会更改相关库存记录的product_id,即将库存记录链接到不同的产品记录。

目前,我在库存模型中定义了各种方法。但正如我所提到的,实际上有多个模型可以引用我的产品表。有什么方法可以在产品模型中定义一些东西吗

? Referenced_by 方法可以告诉所有链接的通过在产品表中查找来处理 prod_code 参数的模型?

class Product < ActiveRecord::Base
  validates_uniqueness_of :prod_code
  referenced_by :prod_code
end

I have what I imagine must be a common situation, but possibly can't find the way to phrase the question to get the solution...

I have a couple of linked models in my application:

class Product < ActiveRecord::Base
  validates_uniqueness_of :prod_code
end

class Stock < ActiveRecord::Base
  belongs_to :product
end

In fact there are a number of other models which also belong to Product. By default, on the stock record, I just see the product_id field, which is an auto-incrementing number that isn't much help to the user. Products have a unique prod_code which is on barcodes etc. and is the natural key of the product database.

What I would like is for the create/edit screens for the stock and other linked models to show a text field for the prod_code, and to be able to respond to parameters in the form stock[prod_code] in a sensible way (e.g. look up the prod_code, and set the prod_id based on the result), and automagically (e.g. Stock.new(params[:stock]) should work.

To clarify, setting stock[prod_code] would not change anything in the product database; it would instead change the product_id for the relevant stock record i.e. link the stock record to a different product record.

At present, I've got various methods defined in the stock model such as prod_code= that make this work. But as I mentioned, there are actually multiple models that refer back to my products table. Is there any way I can define something inside the Product model?

e.g. something like a referenced_by method that would tell all linked models to deal with the prod_code argument by looking it up in the products table?

class Product < ActiveRecord::Base
  validates_uniqueness_of :prod_code
  referenced_by :prod_code
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你的星星会说话 2025-01-02 06:22:33

您可能想要使用的是嵌套表单。

首先,您可以告诉您的模型接受嵌套属性(嵌套属性的 api),就像

accepts_nested_attributes_for :product

您的库存模型一样。

然后在您的表单中,您可以使用 fields_for (api for fields_for) 来嵌套产品字段进入您的库存表格。
想象一下:

= form_for(@stock) do |form|
  = form.text_field :some_stock_attribute
  = form.fields_for(@stock.product) do |product_form|
     = product_form.text_field :prod_code

这本质上将您的 prod_code 表单字段的名称嵌套到“stock[product_attributes][prod_code]”,同时,由于 Accepts_nested_attributes_for,您的 Stock 模型已准备好将 Product_attributes 传递给关联的产品。

更新(参见评论):

您想象的referenced_by方法不存在。此外,您还需要多注意一点,然后只需更改相关产品即可。我认为,如果 prod_code 不存在,您仍然希望显示错误。

粗略地,您可以向库存添加虚拟属性,这允许您的库存表单具有文本字段:prod_code(将 attr:prod_code 添加到库存模型)。

其次,在验证之前,您可能需要一个方法来查找产品的 prod_code,并更改关联,或者在 :prod_code 上添加错误。

这可能看起来像这样:

class Stock
  attr :prod_code

  before_validation :associate_product_from_prod_code

  def associate_product_from_prod_code
    unless self.product = Product.where(:prod_code => prod_code)
      errors[:prod_code] << "Product Code is not valid"
    end
  end

end

What you probably want to use are nested forms.

First you can tell your model to accept the nested attributes (api for nested attributes),like

accepts_nested_attributes_for :product

in your stock model.

Then in your form you can use fields_for (api for fields_for), to nest product fields into your stock form.
Imagine something like:

= form_for(@stock) do |form|
  = form.text_field :some_stock_attribute
  = form.fields_for(@stock.product) do |product_form|
     = product_form.text_field :prod_code

This essentially nests the name of your prod_code form field, to "stock[product_attributes][prod_code]", while, thanks to the accepts_nested_attributes_for, your stock model is prepared to pass the product_attributes to the associated product.

Update (see comments):

A referenced_by method as you imagine, does not exist. Also you will have to take care of a bit more, then just changing the associated product. You will still want to display an error, if the prod_code doesn't exist, i assume.

Roughly you can add a virtual attribute to the stock, which allows your stock form to have a text_field :prod_code (add attr :prod_code to stock model).

Second, before validation you will probably need a method, that looks up the product for the prod_code, and changes the association, or adds an error on :prod_code.

this could look a bite like this:

class Stock
  attr :prod_code

  before_validation :associate_product_from_prod_code

  def associate_product_from_prod_code
    unless self.product = Product.where(:prod_code => prod_code)
      errors[:prod_code] << "Product Code is not valid"
    end
  end

end
只等公子 2025-01-02 06:22:33

您可以使用 prod_code 而不是 Product_id 作为foreign_key:

class Stock
  belongs_to :product, :foreign_key => "prod_code"
end

或者您可以在 Product 上设置它:

class Product
  has_many :stocks, :foreign_key => "prod_code"
  has_many :images, :foreign_key => "prod_code"
  has_many :sizes,  :foreign_key => "prod_code"
end

You could use prod_code instead of product_id as the foreign_key:

class Stock
  belongs_to :product, :foreign_key => "prod_code"
end

Or you can set it on Product:

class Product
  has_many :stocks, :foreign_key => "prod_code"
  has_many :images, :foreign_key => "prod_code"
  has_many :sizes,  :foreign_key => "prod_code"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文