的關係我有一個現有的關係,其中導軌 - 切換到多態
Artwork has_many :photos
和Photo belongs_to :artwork
我一直在努力改變這些關係是:
Artwork has_many :photos, as: :attached_photo
和Photo belongs_to :attached_photo, polymorphic: true
我從Artwork表單中創建Photo對象,但它們不是嵌套的資源。
的型號如下:
class Artwork < ActiveRecord::Base
attr_accessible :photos_attributes
has_many :photos, as: attached_photo
accepts_nested_attributes_for :photos, allow_destroy: true
end
class Photo < ActiveRecord::Base
attr_accessible :photo, :artwork_id, :attached_photo_id, :attached_photo_type
belongs_to :attached_photo, polymorphic: true
has_attached_file :photo,
path: ":rails_root/assets/:class/:id_partition/:style/:basename.:extension",
url: "/:class/:id/:style/:basename.:extension"
end
出於測試目的,我都留在照片模式單一關係和多態關係領域。我填寫了數據庫中的兩組字段,以便它們指向相同的資源。
當我用多態關聯所需的版本替換兩個模型之間的關係時,我視圖中的所有圖像都停止顯示。我的看法用於顯示圖片的代碼是
<%= image_tag(@artwork.photos.first.photo.url(:medium)) %>
當製作的關係類型之間的變化,在添加了多態關聯的URL仍然正確顯示在頁面上,但由於某些原因,圖像顯示爲被打破。在嘗試直接瀏覽到圖像中的一個,我收到錯誤消息是:
undefined method `artwork' for #<Photo:0xb3fc2b74>
我能不明白的地方的照片模型期待一個藝術品的方法從何而來。任何人都可以爲我澄清?