Ruby on rails 狂欢关系问题
我正在尝试向Spree商店添加产品附件功能。例如,Ruby on rails 狂欢关系问题,ruby-on-rails,spree,Ruby On Rails,Spree,我正在尝试向Spree商店添加产品附件功能。例如,产品有许多附加的文档:小册子、说明手册等。我无法使文档和产品之间的关系发挥作用 我可以使用回形针宝石作为附件功能,因为Spree已经将其用于图像 我有“文档”模型:models/spree/document.rb: class Spree::Document < ActiveRecord::Base belongs_to :products, class_name: "Spree::Product" has_attached_fil
产品
有许多附加的文档
:小册子、说明手册等。我无法使文档和产品之间的关系发挥作用
我可以使用回形针宝石作为附件功能,因为Spree已经将其用于图像
我有“文档”模型:models/spree/document.rb
:
class Spree::Document < ActiveRecord::Base
belongs_to :products, class_name: "Spree::Product"
has_attached_file :pdf
end
Spree::Product.class_eval do
has_many :documents, dependent: :destroy
end
然后我添加迁移:
class CreateDocuments < ActiveRecord::Migration
def change
create_table :spree_documents do |t|
t.timestamps
end
end
end
class AddPdfToDocuments < ActiveRecord::Migration
def self.up
add_attachment :spree_documents, :pdf
end
def self.down
remove_attachment :spree_documents, :pdf
end
end
似乎我没有正确定义文档和产品之间的关系,但我不确定问题出在哪里。看起来您从未在
Spree::documents
表中添加product\u id
列。当您定义一个模型属于另一个模型时,它会告诉ActiveRecord第一个模型将是其表中的[relation]\u id
列
您只需要确保在迁移中添加t.references:product
,这样看起来像:
class CreateDocuments < ActiveRecord::Migration
def change
create_table :spree_documents do |t|
t.references :product
t.timestamps
end
end
end
class CreateDocuments
class CreateDocuments < ActiveRecord::Migration
def change
create_table :spree_documents do |t|
t.references :product
t.timestamps
end
end
end