您好,请任何人帮助我与协会?我有一篇带有一个预览图像和许多文章图像的文章。图像可用于许多文章。所以我的模型是:
class Article
has_many :article_images
has_many :main_images, :class_name => "Image", :through => :article_images
has_one :preview_image, :class_name => "Image", :through => :article_images
end
class ArticleImage
belongs_to :article
belongs_to :preview_image, :class_name => "Image", :foreign_key => :image_id, :conditions => ["images.itype = 'preview_image'"]
belongs_to :main_image, :class_name => "Image", :foreign_key => :image_id, :conditions => ["images.itype = 'main_image'"]
end
class Image < ActiveRecord::Base
has_many :article_images
has_many :articles
end
问题是使用这段代码我得到了错误:
ActiveRecord::HasOneThroughCantAssociateThroughCollection: Cannot have a has_one :through association 'Article#preview_image' where the :through association 'Article#article_images' is a collection. Specify a has_one or belongs_to association in the :through option instead
如果我在文章中为 preview_image 创建一个新关联,如下所示:
has_one :article_image
has_one :preview_image, :class_name => "Image", :through => :article_image
似乎无法正常工作。有人可以建议我一个解决方案吗
提前致谢