我遵循Rails指南来覆盖引擎模型,我使用ActiveSupport::Concern
来覆盖它
我在seven_gallery/lib/concerns/models/gallery.rb
中的引擎内部创建了一个模块,其中包含以下代码:
module SevenGallery::Concerns::Models::Gallery
extend ActiveSupport::Concern
included do
has_many :photos, dependent: :destroy
default_scope { order("created_at desc") }
end
end
并将seven_gallery/app/models/seven_gallery/gallery.rb
代码更改为:
module SevenGallery
class Gallery < ActiveRecord::Base
include SevenGallery::Concerns::Models::Gallery
end
end
现在在我的主机应用程序中,我已经通过将我的引擎包含在Gemfile
中
gem "seven_gallery", path: "../seven_gallery"
我有一个User
模型,它包含:
class User < ActiveRecord::Base
has_one :gallery, class_name: SevenGallery::Gallery
end
但是,每当我运行应用程序时,我都会在User
模型中的唯一一行出现此错误:
uninitialized constant Concerns::Models
致任何面临同样问题的人。事实证明,这是因为文档不够清晰,而且有一些缺陷。
请查看此帖子https://groups.google.com/forum/#!topic/rubyonrails docs/Oo68KwRdwyo