扩展Spree ::产品型号/类别 [英] Extending the Spree::Product model/class

查看:74
本文介绍了扩展Spree ::产品型号/类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的Rails 3.2应用程序中使用Spree,并且我想扩展Spree的Product类以更好地满足我的需求,例如在我的应用程序中与另一个模型建立关系。最好的方法是什么?我在项目文档中找不到有关此内容的任何信息

I'm using Spree in a Rails 3.2 app of mine and I want to extend Spree's Product class to better suit my needs as for example to establish a relationship with another model in my app. What's the best way to do this? I could not find anything about it in the project documentation

如果要向产品资源添加新的属性/字段怎么办?我也找不到它的迁移方式:/

And what if I want to add new attributes/fields to the Product resource? I can't find it's migration either :/

预先感谢:)

推荐答案

此处最好的做法是在您的应用中创建一个product_decorator.rb。

The best thing to do here is to create a product_decorator.rb in your app.

如下所示:

Spree::Product.class_eval do

end

在这里,您可以随意进行修改

In there, you can feel free to modify whatever you want!

以下是该文档:



https://guides.spreecommerce.com/developer/logic.html


要将新字段添加到现有模型中,请执行以下迁移:

To add a new field to an already existing Model, run a migration like this:

class AddSubscribableFieldToVariants < ActiveRecord::Migration
  def change
    add_column :spree_variants, :subscribable, :boolean, default: false
  end
end

然后在模型中添加以下内容:

And then in the model add following:

Spree::Variant.class_eval do
  attr_accessible :subscribable
end

这篇关于扩展Spree ::产品型号/类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆