你好,請任何人幫助我與協會?我有一篇預覽圖片和很多文章圖片。圖片可以用於很多文章。所以,我的型號有:has_one通過有很多
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
似乎不能正常工作。 可有人請建議我一個解決方案
在此先感謝
是的,這將工作,但我寧願不更改數據庫schema.I使用rails應用程序只從數據庫讀取數據。不管怎樣,謝謝你 – skalogirou