我对rails 4的问题有疑问。我写了一个问题:
module Imageable
extend ActiveSupport::Concern
included do
belongs_to :imageable
has_many :images, foreign_key: 'imageable_id', dependent: :destroy
# has_one :primary_image, ->( limit(1) ), foreign_key: imageable_id, class_name: 'Image'
accepts_nested_attributes_for :images, reject_if: proc { |attributes| attributes['source'].blank? }, allow_destroy: true
after_initialize :build_image
end
private
def build_image
self.build_imageable if self.imageable.present? && self.new_record?
self.images.build if self.images.blank? && self.new_record?
end
end
我的私有build_image方法有问题。此方法的第一行引发错误:
undefined method `all' for Imageable:Module
当这个代码在模型内部时,并不关心一切正常。有什么想法吗?